12

I'm using UIImagePNGRepresentation to save an image. The result image is of size 30+ KB and this is BIG in my case.

I tried using UIImageJPEGRepresentation and it allows to compress image, so image saves in < 5KB size, which is great, but saving it in JPEG gives it white background, which i don't want (my image is circular, so I need to save it with transparent background).

How can I compress image size, using UIImagePNGRepresentation?

Hyder
  • 1,163
  • 2
  • 13
  • 37
  • 1
    The simple answer is you can not, until you choose to compromise your image dimension. `UIImagePNGRepresentation` returns the least possible compressed size for the image and as this is a lossless compression method, it can't compromise the quality of image, unlike JPEG. So only possibility is to reduce image dimension. – Ayan Sengupta Feb 11 '16 at 20:00
  • 2
    @AyanSengupta That's not really true, there are multiple ways for a PNG image to get a better compression (different inflate settings, different settings used, a palette) but to choose the best one, we would have to try them all. There are special tools to optimize PNG images but we can't expect a simple graphic encoder to do that. – Sulthan Feb 11 '16 at 20:12
  • 1
    If your image doesn't have a lot of colors, you can go with PNG-8 (PNG with a 256 color palette) which is usually smaller. It might be difficult to force UIImagePNGRepresentation to produce such format so you might want to try converting `UIImage` to rgb data and then try a different PNG encoder, e.g. libpng. In the end it all depends on the image you have. – Sulthan Feb 11 '16 at 20:17
  • 1
    @Sulthan, you are right. But I think we were talking about some different contexts. The user has asked if we could use `UIImagePNGRepresentation` to reduce image size and there is definitely no way we can do this with that API. Obviously there are some third party APIs available that might be used, instead. Unfortunately, native `UIImagePNGRepresentation` has already done it's best :( – Ayan Sengupta Feb 11 '16 at 20:22
  • Thank you guys for the information! So what I believe is that I need to reduce the dimensions to reduce the size. My current dimensions are 118x118. Gotta try something smaller :| – Hyder Feb 11 '16 at 20:45
  • @Sha have you tried my solution? I am reducing its height/2 and width/2 after checking if its greater than 5KB. – Shehzad Ali Feb 12 '16 at 06:49

2 Answers2

2

PNG uses lossless compression, that's why UIImagePNGRepresentation does not accept compressionQuality parameter like UIImageJPEGRepresentation does. You might get a bit smaller PNG file with different tools, but nothing like with JPEG.

Filip Radelic
  • 26,607
  • 8
  • 71
  • 97
0

May be this will help you out:

- (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
    }
}

Resizing 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;
}
Shehzad Ali
  • 1,846
  • 10
  • 16