Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce UIImage size to a manageable size (reduce bytes)

I want to reduce the number of bytes of an image captured by the device, since i believe the _imageScaledToSize does not reduce the number of bytes of the picture (or does it?) - i want to store a thumbnail of the image in a local dictionary object and can't afford to put full size images in the dictionary. Any idea?

like image 848
Mustafa Avatar asked Jan 28 '09 11:01

Mustafa


2 Answers

If you wish to simply compress your UIImage, you can use

NSData *dataForPNGFile = UIImagePNGRepresentation(yourImage);

to generate an NSData version of your image encoded as a PNG (easily inserted into an NSDictionary or written to disk), or you can use

NSData *dataForPNGFile = UIImageJPEGRepresentation(yourImage, 0.9f);

to do the same, only in a JPEG format. The second parameter is the image quality of the JPEG. Both of these should produce images that are smaller, memory-wise, than your UIImage.

Resizing a UIImage to create a smaller thumbnail (pixels-wise) using published methods is a little trickier. _imageScaledToSize is from the private API, and I'd highly recommend you not use it. For a means that works within the documented methods, see this post.

like image 147
Brad Larson Avatar answered Oct 12 '22 23:10

Brad Larson


I ran into this problem the other day and did quite a bit of research. I found an awesome solution complete with code here:

http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

like image 28
radesix Avatar answered Oct 12 '22 23:10

radesix