How do I compress an image from a UIImageView?

I am using image from UIImageView * image to upload to server. I am using code like NSData *imageToUpload = UIImageJPEGRepresentation(image.image, 90);

but I would like to compress the image to less than 2MB. How do I compress an image from a UIImageView?

+3


source to share


1 answer


Here is the code snippet 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 it helps !!!

+5


source







All Articles