Compress UIImage by reducing bits per pixel in Swift [duplicate]












0
















This question already has an answer here:




  • Convert an image to a 16bit color image

    1 answer




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










share|improve this question















marked as duplicate by Cristik, Billal Begueradj, stealthyninja, Gerhard Barnard, EdChum Nov 14 '18 at 8:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • no. They are all using UIImage.jpegData(compressionQuality: CGFloat) , I need the alpha channel.

    – Danny182
    Nov 13 '18 at 21:50
















0
















This question already has an answer here:




  • Convert an image to a 16bit color image

    1 answer




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










share|improve this question















marked as duplicate by Cristik, Billal Begueradj, stealthyninja, Gerhard Barnard, EdChum Nov 14 '18 at 8:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • no. They are all using UIImage.jpegData(compressionQuality: CGFloat) , I need the alpha channel.

    – Danny182
    Nov 13 '18 at 21:50














0












0








0


1







This question already has an answer here:




  • Convert an image to a 16bit color image

    1 answer




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










share|improve this question

















This question already has an answer here:




  • Convert an image to a 16bit color image

    1 answer




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





This question already has an answer here:




  • Convert an image to a 16bit color image

    1 answer








swift uiimage compression png






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 10:10







Danny182

















asked Nov 13 '18 at 19:57









Danny182Danny182

1,0421219




1,0421219




marked as duplicate by Cristik, Billal Begueradj, stealthyninja, Gerhard Barnard, EdChum Nov 14 '18 at 8:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Cristik, Billal Begueradj, stealthyninja, Gerhard Barnard, EdChum Nov 14 '18 at 8:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • no. They are all using UIImage.jpegData(compressionQuality: CGFloat) , I need the alpha channel.

    – Danny182
    Nov 13 '18 at 21:50



















  • no. They are all using UIImage.jpegData(compressionQuality: CGFloat) , I need the alpha channel.

    – Danny182
    Nov 13 '18 at 21:50

















no. They are all using UIImage.jpegData(compressionQuality: CGFloat) , I need the alpha channel.

– Danny182
Nov 13 '18 at 21:50





no. They are all using UIImage.jpegData(compressionQuality: CGFloat) , I need the alpha channel.

– Danny182
Nov 13 '18 at 21:50












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer
























  • 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).

    – Danny182
    Nov 14 '18 at 7:11


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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.






share|improve this answer
























  • 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).

    – Danny182
    Nov 14 '18 at 7:11
















1














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.






share|improve this answer
























  • 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).

    – Danny182
    Nov 14 '18 at 7:11














1












1








1







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 3:19









Mark AdlerMark Adler

58k764111




58k764111













  • 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).

    – Danny182
    Nov 14 '18 at 7:11



















  • 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).

    – Danny182
    Nov 14 '18 at 7:11

















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).

– Danny182
Nov 14 '18 at 7:11





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).

– Danny182
Nov 14 '18 at 7:11



Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation