0

I'm trying to obtain a PNG image with a resolution 512px x 512px smaller than 100 kB.
At the moment the files are around 350 kB. I'm trying to reduce the file size and a way I was thinking is to reduce the Color-depth.

This is the information of the PNG images:

bits per component -> 8
bits per pixel -> 32

I wrote some code to create the CGContext with a different bits per Pixel, but I don't think that's the write way.
I don't want to use the UIImage.jpegData(compressionQuality: CGFloat) since I need to maintain the alpha channel.
I already found some code in Objective-C but that didn't help.
I'm looking for a solution in Swift

DanielZanchi
  • 2,708
  • 1
  • 25
  • 37

1 Answers1

0

You would need to decimate the original image somehow, e.g. zeroing some number of the least significant bits or reducing the resolution. However that completely defeats the purpose of using PNG in the first place, which is intended for lossless image compression.

If you want lossy image compression, where decimation followed by PNG is one approach, then you should instead use JPEG, which makes much more efficient use of the bits to reproduce a psycho-visually highly similar image. More efficient than anything you or I might come up with as a lossy pre-processing step to PNG.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
  • 1
    I need the alpha channel so I can’t use jpeg. I can’t change the resolution (need 512 x 512) so I’m looking for a way to reduce the bit depth in swift (that was the question). – DanielZanchi Nov 14 '18 at 07:11