I have a really strange problem…
In my iPhone app, the user can open an image from the camera roll, in my example an
wallpaper with 1920 x 1080 px (72 dpi).
Now, a want to resize the image to a width of e.g. 1024 px:
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(@"New image has w=%f, h=%f", newImage.size.width, newImage.size.height);
return newImage;
}
With the log message I can check, that the width is 1024 and the height is 576. Everything is all right!
But now, I save the image in the Documents folder:
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@.jpg", uniqueId]];
[UIImageJPEGRepresentation(originalImage, 1.0) writeToFile:jpgPath atomically:YES];
And now a very strange effect:
a) When I use the Retina Simulator, the saved image at “/Users/[…]/Library/Application Support/iPhone Simulator/5.1/Applications/[…]/Documents/” has a size of 1,5 MB and the resolution is 2048 x 1152 px at 144(!) dpi.
b) When I use the normal Simulator, the size is 441 KB and the resolution is 1024 x 768 px at 72 dpi.
How can a force to save an UIImage with 72 dpi?
ARGHHHH!!!
I got it…
“1.0” instead of “0.0”!
The 3th parameter is the scale factor to apply to the bitmap. If you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen.