使用ImageIO在iOS上创建渐进式jpeg会在设备上产生块效果

Jor*_*hen 11 iphone cocoa uiimage ios javax.imageio

我正在尝试从UIImage对象创建渐进式jpeg ,这是我的代码

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);
Run Code Online (Sandbox Code Playgroud)

这在模拟器中运行时效果很好,但不幸的是在设备上产生了粗糙/块状结果:

矮胖/块状的结果.

关于发生了什么的任何想法?我将恢复使用UIImageJPEGRepresentation作为最后的手段,我真的需要渐进式JPEG.

Dav*_*d H 0

好消息:我刚刚在 iPhone 4 上进行了测试,图像看起来很棒:

NSDictionary *jfifProperties = [NSDictionary dictionaryWithObjectsAndKeys:
@72, kCGImagePropertyJFIFXDensity,
@72, kCGImagePropertyJFIFYDensity,
@1, kCGImagePropertyJFIFDensityUnit,
nil];
Run Code Online (Sandbox Code Playgroud)

(使用新的文字语法)。

关于这些密度选项含义的一个很好的JFIF 参考。