UIImageJPEGRepresentation and UIImagePNGRepresentation to NSData on iOS SDK 5

Originator:amar
Number:rdar://10699365 Date Originated:16-01-2012
Status:Open Resolved:No
Product:iOS SDK Product Version:5.0.1
Classification:Serious bug Reproducible:Always
 
Summary:

UIImageJPEGRepresentation and UIImagePNGRepresentation are increasing size of files saved through conversion to NSData by several times. For example jpg image that is saved inside of project that takes 1.5 MB, will after conversion to NSData with UIImageJPEGRepresentation take almost 6 MB of space which is 4 times bigger than the source image while PNG will take even more because it's not compressed, but still it shouldn't take more than original one, because no effects or alterations have been done to the original image.

This is absolutely unacceptable for any kind of web service client where you need to send pictures which prior sending must be converted to base64 which by itself make file larger about 33% so in the end file size will  

Steps to Reproduce:

Create empty project, add one or more images, convert them to NSData and save to disk. Compare results.

Expected Results:

If source image is jpg then jpg saved trough conversion from UIImage to NSData with  UIImageJPEGRepresentation should be exactly the same size using compression factor 1 (best quality) or smaller when using lower compression factors. UIImagePNGRepresentation should return slightly larger file because it's not compressed but still not 6x larger.

Actual Results:

New created files will be ~4 times larger for jpg and ~6 times larger for png format.

Regression:

Notes:

Please advise if there is another way to get the NSData with actual file size from UIImage.

First file results with 106% larger image when saved as JPG and 257% larger when saved as PNG, while the second file results with 98.9% larger image as JPG and 226.5% larger image when saved as PNG.

Comments

I had the same problem and found the solution - don't use UIImagePNGRepresentation when converting image to NSData. Here the sample how you have to do to have NSData size the same as your image file

NSString path=[[NSBundle mainBundle] pathForResource:@"foto14" ofType:@"jpg"]; NSData imageData = [NSData dataWithContentsOfFile:path];

By yuri.scooter at May 15, 2012, 8:26 a.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!