8

I need to be able to compress PNG files at run-time but can't find any libraries that are iOS ready that can do that. JPEG does is not a great fit for me because I need to the Alpha channel that PNG provides, and JPEG does not. Turning PNG crush into something I can use within my iOS project is probably beyond my skill/knowledge set when it comes to C.

Just to be extra-clear, I need to do this at run-time, not compile-time.

Eugene Trapeznikov
  • 3,220
  • 6
  • 47
  • 74
Rob Reuss
  • 1,400
  • 10
  • 20
  • i think this can help you. http://stackoverflow.com/questions/4394491/how-to-compress-resize-image-on-iphone-os-sdk-before-uploading-to-a-server – Red Mak Mar 19 '12 at 22:54
  • What format is the data in before you want to save it? And do you care about getting off the main thread? – Tommy Mar 19 '12 at 22:59
  • @Malek Thanks, but that item focuses on resizing the image, not compressing it.. – Rob Reuss Mar 21 '12 at 19:47
  • @Tommy The images start as an UIImage (they are screenshots) and need to be saved as PNG to preserve the Alpha channel. It would be best if any library I used could run on a background thread. The compression activity will occur in an NSOperation and I can put it on the main thread if necessary. – Rob Reuss Mar 21 '12 at 19:48

3 Answers3

3

Try ImageIO. See ImageIO Programming Guide

Caleb
  • 124,013
  • 19
  • 183
  • 272
EricS
  • 9,650
  • 2
  • 38
  • 34
  • Thanks for pointing Image I/O out - might be useful for other projects. For this project, I'm looking to compress PNG files, which is not something that PNG natively supports as a part of image format. In the Image I/O documentation, you can see that none of the PNG Dictionary Keys offer control over compression (http://developer.apple.com/library/ios/#documentation/graphicsimaging/Reference/CGImageProperties_Reference/Reference/reference.html#//apple_ref/doc/uid/TP40005103) - if it did, UIImagePNGRepresentation would prob provide an interface for it. – Rob Reuss Mar 21 '12 at 19:41
  • 1
    @RobReuss I'm a little confused about this; PNGs are compressed but there are multiple ways of encoding a PNG — hence why pngcrush and its like are often able to achieve better compression by simply spending much more time weighing up the alternatives. So what you want is an encoder that you can tell to spend more time finding a better compression ratio rather than one you just have to trust to use its default time/efficiency trade off? – Tommy Mar 21 '12 at 20:43
  • 1
    Sorry if I've been unclear. You're right - PNG does have compression - but iOS does not expose a parameter that allows you to control the degree of compression that is used. What I'm looking for is a library that I could build into project that would enable me to take a UIImage and produced a more compressed PNG than is produced by UIImagePNGRepresentation. If there was a way to use pngcrush at run-time, that would answer my need. – Rob Reuss Mar 24 '12 at 03:04
  • PNG is useful for lossless image compression, the type of space savings done by tools like pngcrush are generated by brute force searching to see if different representations can generate a smaller file size. There is not a "degree of compression" setting for PNGs like you would find in a lossy format like JPEG. – MoDJ May 13 '19 at 21:38
1

Have you tryed UIImagePNGRepresentation?

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/UIImagePNGRepresentation

Eduardo Costa
  • 1,974
  • 1
  • 16
  • 22
1

If you really have no other way maybe you can use UIImagePNGRepresentation in a way such that it save a thinner png file :

// load image from the one you created
UIImage *image = [UIImage imageNamed:@"your image name"];
// give it a new PNG representation from the API
NSData *pngImage = UIImagePNGRepresentation(image);
//save it to another place
NSError *error = nil;
[pngImage writeToFile:@"your path" options:NSDataWritingAtomic error:&error];

And then see if the new image is thinner or not. This method works on a mac : just opening PNG files and exporting them to another PNG file could reduce it's size.

Dulgan
  • 6,674
  • 3
  • 41
  • 46
  • I think you may have misunderstood section 13.2. The purpose of that exclusion is to justify rejecting apps where the sole purpose is battery drain or heat generation - believe it or not, people submit that kind of thing. You should not be shy about using the device's CPU to your hearts delight if it results in a great user experience - this is especially true on the iPad where one is less concerned about protecting the a fundamental function of the device (like the iPhone needing to make calls). – Rob Reuss Mar 28 '12 at 02:23
  • That's an interesting idea about re-saving the PNG though - I seriously doubt it'll give me the compression level I'm looking for, since it's just a side-effect, but you got me curious and I'll test it. – Rob Reuss Mar 28 '12 at 02:24
  • Ok thanks for this precision about this section, maybe I didn't catch the thing because I'm not bilingual. At least, I didn't think about people making such useless Apps... Anyway I removed this part of the answer. Please tell us about your test. – Dulgan Mar 28 '12 at 06:47