1

So I have a UIImage that I load with [UIImage imageNamed]. I then save that file to my documents directory. My issue is that the file originally had 240 resolution. When I retrieve the file from the Simulator's documents directory, it now has 72 resolution. How can I save it at the same resolution it was loaded as?

Here is how I load it:

UIImage * image = [UIImage imageNamed:@"halloween_big.jpg"]
CGContextRef context = CGBitmapContextCreate(
                                                     (void*) pixels,
                                                     image.size.width,
                                                     image.size.height,
                                                     8,
                                                     image.size.width * 4,
                                                     CGImageGetColorSpace(image.CGImage),
                                                     kCGImageAlphaPremultipliedLast
                                                     );

//  get the image from the current context with our new pixels
CGImageRef imageRef = CGBitmapContextCreateImage( context );

image = [UIImage imageWithCGImage:imageRef];

EDIT - I have also tried by loading the image with just this and making no changes to the image:

 UIImage *loaded = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"halloween_big" ofType:@"jpg"]];  

Here is how I'm saving it:

NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];

EDIT - I get the same loss in resolution if I save the file as a PNG.

Just to be sure, the same thing happens if I change the quality paramter to UIImageJPEGRepresentation.

Brian
  • 3,571
  • 7
  • 44
  • 70
  • I have gotten the same results if I save the image immediately after I load it without dealing with the graphics context. – Brian Nov 07 '11 at 12:31
  • This can be the solution. Have a look over here: http://stackoverflow.com/questions/990550/iphone-save-image-at-higher-resolution-without-pixelating-it – UPT Nov 07 '11 at 13:34
  • Can you elaborate - I'm not trying to change the size of the image, I just want the saved image to be the same resolution as the image I originally opened. – Brian Nov 07 '11 at 14:58

1 Answers1

0

The iPhone is likely loading the image with the optimized resolution for your simulator.

Are you testing on a non-retina-display version of the simulator?

Also:

What dpi resolution is used for an iPhone App?

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • I tried with and without Retina in the iPhone Simulator. I also traced it back to before I do anything with the image. As soon as it is loaded as UIImage - I write it to disk and its resolution is 72. I have tried loading the UIImage with both imageNamed and initWithContentsOfFile. – Brian Nov 07 '11 at 12:19
  • do you see different results on an actual device? – Michael Dautermann Nov 07 '11 at 12:24
  • I have not tried it on a device yet. I guess that is a possibility. I'm still in early development trying to make a prototype. – Brian Nov 07 '11 at 12:29