问题描述
我正在尝试从 UIImage
对象创建渐进式 jpeg,这是我的代码
I'm trying to create a progressive jpeg from a UIImage
object, this is the code i'm
NSMutableData *data = [NSMutableData data];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent: @"Library/Caches/test.jpg"];
CFURLRef url = CFURLCreateWithString(NULL, (CFStringRef)[NSString stringWithFormat:@"file://%@", path], NULL);
CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypeJPEG, 1, NULL);
CFRelease(url);
NSDictionary *jfifProperties = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge id)kCFBooleanTrue, kCGImagePropertyJFIFIsProgressive,
nil];
NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:.7], kCGImageDestinationLossyCompressionQuality,
jfifProperties, kCGImagePropertyJFIFDictionary,
nil];
CGImageDestinationAddImage(destination, ((UIImage*)object).CGImage, (__bridge CFDictionaryRef)properties);
CGImageDestinationFinalize(destination);
CFRelease(destination);
这在模拟器中运行时效果很好,但不幸的是在设备上产生了块状/块状结果:
This works great when running in simulator, but unfortunately produces chunky/blocky results on device:
对正在发生的事情有什么想法吗?我会恢复使用 UIImageJPEGRepresentation
作为最后的手段,我真的需要渐进式 JPEG.
Any ideas on what's going on? i'd revert to using UIImageJPEGRepresentation
as a last resort, I really need progressive JPEGs.
推荐答案
好消息:我刚刚在 iPhone 4 上测试了这个,图像看起来很棒:
Good news: I just tested this on an iPhone 4 and the image looks great:
NSDictionary *jfifProperties = [NSDictionary dictionaryWithObjectsAndKeys:
@72, kCGImagePropertyJFIFXDensity,
@72, kCGImagePropertyJFIFYDensity,
@1, kCGImagePropertyJFIFDensityUnit,
nil];
(使用新的文字语法).
(using new literal syntax).
很好的JFIF 参考,了解这些密度选项的含义.
A good JFIF reference for what these density options mean.
这篇关于使用 ImageIO 在 iOS 上创建渐进式 jpeg 会在设备上产生块状结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!