Compress UIImage by reducing bits per pixel in Swift [duplicate]
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
swift uiimage compression png
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.
add a comment |
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
swift uiimage compression png
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
add a comment |
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
swift uiimage compression png
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
swift uiimage compression png
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
no. They are all using UIImage.jpegData(compressionQuality: CGFloat) , I need the alpha channel.
– Danny182
Nov 13 '18 at 21:50