Creating progressive jpeg on iOS with ImageIO produces blocky results on device

会有一股神秘感。 提交于 2019-12-05 16:30:10

问题


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:

Any ideas on what's going on? i'd revert to using UIImageJPEGRepresentation as a last resort, I really need progressive JPEGs.


回答1:


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).

A good JFIF reference for what these density options mean.




回答2:


On iOS simulator its okay as mentioned by you.

But have you tested on IOS simulator with Retina Display. For this do the following steps: 1. Launch Simulator 2. Go to "Hardware" --> "Device" 3. Select a Simulator with Retina Display and check it again.

If you encounter the problem on ios retina simulator then, you got the catch.

Try to use a image of very small resolution and then test it on your device.

There are few issues to show very high resolution image in UIImageView. Please check the link mentioned below:- High Resolution Image in UIImageView



来源:https://stackoverflow.com/questions/10829100/creating-progressive-jpeg-on-ios-with-imageio-produces-blocky-results-on-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!