11

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:

chunky/blocky result.

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

Daniel
  • 23,129
  • 12
  • 109
  • 154
Jorge Cohen
  • 1,512
  • 10
  • 34
  • @ShivanRaptor it's been tested with iOS 5.0 - 5.1.1 – Jorge Cohen May 31 '12 at 07:34
  • I'm trying to test the codes with my phone. Can you also share the `test.jpg` you are using? – Raptor May 31 '12 at 07:35
  • @ShivanRaptor I don't have that image anymore, basically this happens with every image i've used so far. – Jorge Cohen May 31 '12 at 07:42
  • What hardware device of the simulator are you running it in? If you are running it in wrong hardware device this can occur sometimes if you have not provided both the standard image and the high res image (Retina provide an extra version of each image with @2x at the end of each image name but before the extension.) you can change the device hardware for the simulator by going to IOS Simulator tool bar at the top of the screen and selecting Hardware >> Device >> (Device you want to run on here). – Popeye Jun 26 '12 at 10:23
  • ran the same code, got the same result; works in simulator, looks like the above when running on the device. iPhone 4, iOS 5.1 – n13 Jul 24 '12 at 07:49
  • Would this have something to do with the way XCode compresses images for device builds? You could turn off image compression in the project settings and test to see if that is the problem or not. – David van Dugteren Jul 26 '12 at 01:04
  • Hezi, it would be great if you would accept my answer as the code does work on the device itself. Other people have been searching for this same question and if it was answered it would help them. If you want to self answer too, that's fine - copy and paste my answer - but just close this out with an answered question. Thanks! – David H Aug 26 '12 at 18:37

2 Answers2

0

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

Community
  • 1
  • 1
NNikN
  • 3,720
  • 6
  • 44
  • 86
0

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.

David H
  • 40,852
  • 12
  • 92
  • 138