Coder
Coder

Reputation: 1681

UIImagePNGRepresentation increasing output file size instead of reducing the size

If we convert a JPEG file using UIImagePNGRepresentation, then the new file size is more. If we convert a PNG file using UIImagePNGRepresentation, then the new file size is less.

Why converting JPEG to PNG is costlier here?

Thanks Jithen

Upvotes: 3

Views: 1034

Answers (1)

Vishnu
Vishnu

Reputation: 2243

To reduce the image size u can use following code

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]];//image is my UIImageview

Hop this Helps !!!

Upvotes: 3

Related Questions