tags:

views:

31

answers:

1

Hi guys,

I'm facing a problem when saving an UIImage, instanciated from data received from an HTTP request, in a file : the file is HUGE, way heavier than the size of the data returned by the HTTP request.

For example, when downloading an image from a given URL, the retrieved data length is around 1.2MB. I then instanciate my UIImage with...

UIImage *myImage = [UIImage imageWithData:responseData];

...and then save the image to the disk with this :

NSData *imageData = UIImageJPEGRepresentation(myImage, 1.0);

[imageData writeToFile:filePath atomically:YES];

The resulting file weight is around 5MB !

I know I could just save the received data to the file instead of first instanciating an UIImage and then retrieve its data back with UIImageJPEGRepresentation, but I'm curious to know why the file size is so huge when using this method.

Any hint ?

Thanks by advance,

Eric.

EDIT :

I can confirm that saving the response data to the file instead of first instanciating an UIImage solve the problem and generate a file of around the same size than the data. Still, I wonder why the size change so much when saving from an UIImage...

+1  A: 

Hi,

I can only assume that the jpeg of your particular image is less efficient that the raw data - this is probably because you've set the quality to 1.0 - try setting the quality parameter to a lower number (0.7 is fine IMHO) and see the file size drop :)

Sam

PS It would be interesting to see what difference this actually makes - if you try it can you post the results here as well - thanks!

deanWombourne
Using 0.7, my file size is around the same as the data length received from the request. What bother me here is that is the file downloaded was already jpeg compressed (which is probably the case), apply a new compression would degrade an already degraded image.
Eric MORAND
You're right, you'd lose some quality. You're doing the right thing just saving the NSData :)
deanWombourne