How to compress image size using UIImagePNGRview - iOS?

I use UIImagePNGRepresentation to save the image. The result image has a size of 30+ KB, and this is BIG in my case.

I tried using UIImageJPEGRepresentation , and it allows you to compress the image, so the image is saved in <Size 5 KB, which is great, but saving it in JPEG gives it a white background that I don't need (my image is circular, so I need to save it with a transparent background )

How can I compress image size using UIImagePNGRepresentation ?

+7
ios objective-c uiimage uiimagejpegrepresentation uiimagepngrepresentation
source share
2 answers

PNG uses lossless compression, so the UIImagePNGR view does not accept a compressionQuality parameter such as UIImageJPEGRepresentation. You can get a slightly smaller PNG file with various tools, but nothing like that with JPEG.

+2
source share

Perhaps this will help you:

 - (void)resizeImage:(UIImage*)image{ NSData *finalData = nil; NSData *unscaledData = UIImagePNGRepresentation(image); if (unscaledData.length > 5000.0f ) { //if image size is greater than 5KB dividing its height and width maintaining proportions UIImage *scaledImage = [self imageWithImage:image andWidth:image.size.width/2 andHeight:image.size.height/2]; finalData = UIImagePNGRepresentation(scaledImage); if (finalData.length > 5000.0f ) { [self resizeImage:scaledImage]; } //scaled image will be your final image } } 

Resize Image

 - (UIImage*)imageWithImage:(UIImage*)image andWidth:(CGFloat)width andHeight:(CGFloat)height { UIGraphicsBeginImageContext( CGSizeMake(width, height)); [image drawInRect:CGRectMake(0,0,width,height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext() ; return newImage; } 
+1
source share

All Articles