I am using image from UIImageView * image to upload to server.
I use code like
NSData *imageToUpload = UIImageJPEGRepresentation(image.image, 90);
but I would like to compress image to be less than 2MB. How to compress image from UIImageView ?
Asked
Active
Viewed 435 times
3

Damir
- 54,277
- 94
- 246
- 365
-
3I think you probably meant 0.9 rather than 90. Decreasing this value further will reduce quality and therefore size. – Bushbert Feb 19 '13 at 03:08
-
4the smaller the number the more compressed and you are doing it right with `UIImageJPEGRepresentation();` but this question has been asked plenty of times before. I am surprised with your rep, that you posted this question... Anyway DUPE question http://stackoverflow.com/a/613380/525576 – John Riselvato Feb 19 '13 at 03:09
1 Answers
5
Here is a piece of code as you need
CGFloat compression = 222.0f;
CGFloat maxCompression = 202.1f;
int maxFileSize = 160*165; //fill your size need
NSData *imageDat = UIImageJPEGRepresentation(image.image, compression);
while ([imageDat length] > maxFileSize && compression > maxCompression)
{
compression -= 0.1222;
imageDat = UIImageJPEGRepresentation(decodedimage.image, compression);
}
NSLog(@"image compressed success");
[image setImage:[UIImage imageWithData:imageDat]];
Hope this Helps !!!

Vishnu
- 2,243
- 2
- 21
- 44