jpg or png for UIImage -- which is more efficient?

10,125

Solution 1

I have an application on the store that needs to save intermediate versions of an image as it's being edited. In the original version, I used PNG format for saving, to avoid quality loss from loading and saving JPEG multiple times.

Sometime around the 2.2 software release, Apple introduced a change into the PNG writing code, such that it takes many times longer to save PNG data from some images. I ended up having to change to saving in JPEG format, because my application was timing out when trying to save images on exit.

Also, you'll run into issues because saving in PNG format doesn't preserve the "orientation" information in the UIImage, so a picture taken in Portrait orientation with the built-in camera will appear rotated after you save and reload it.

Solution 2


I am getting the odd crash, presumably due to out of memory


Then STOP WHAT YOU ARE DOING RIGHT NOW and first figure out if that's actually the cause of the crash. Otherwise there's a very good chance that you're chasing the wrong problem here, fixing a memory problem that doesn't exist while ignoring the real cause of the crash. If you want to fix a crash, start by figuring out what caused the crash. Following up on what's "presumably" the problem is a recipe for wasted time and effort.

Solution 3

It depends on what type of images you're dealing with. If you're dealing with photographic images, JPEGs will almost always be smaller than PNGs, with no discernable loss of detail as can be seen by the human eye.

Conversely, if you're dealing with highly non-photographic images such as GUI elements or images with large blocks of solid colors, then PNGs and JPEGs will be comparable in size, but the PNG will save losslessly whereas the JPEG will be lossy and have very visible artifacts. If you have a really simple image (very large blocks of constant colors, e.g.), then a PNG will very likely be much smaller than a JPEG, and again will not have any compression artifacts.

The act of saving an image as a PNG or JPEG should not take up very much transient memory. When an image is in memory, it is typically stored uncompressed in memory so that it can be drawn to the screen very quickly, as opposed to having to decompress it every time you want to render it. Compared to the size of the uncompressed image, the amount of extra temporary storage you need to compress it is very small. If you can fit the uncompressed image in memory, you don't have to worry about the memory used while compressing it.

And of course, once you write the image to the file system in non-volatile storage and free the in-memory image, it really doesn't matter how big the compressed image is, because it doesn't take up main memory any more. The size of the compressed image only affects how much flash storage it's using, which can be an issue, but it does not affect high likely your app is to run out of memory.

Solution 4

Your crashes may be from a known memory leak in the UIImagePickerController.

This should help you fix that.

Share:
10,125
Stephen Petschulat
Author by

Stephen Petschulat

Updated on June 25, 2022

Comments

  • Stephen Petschulat
    Stephen Petschulat almost 2 years

    I am grabbing an image from the camera roll and then using it for a while as well as save it to disk as a PNG on the iPhone. I am getting the odd crash, presumably due to out of memory.

    Does it make a difference if I save it as PNG or JPG (assuming I choose note to degrade the quality in the JPG case)? Specifically:

    • is more memory then used by the UIImage after I reload it off of disk if I saved it as a PNG?
    • is it possible the act of saving as PNG uses up more memory transiently during the saving process?

    I had been assuming the UIImage was a format neutral representation and it shouldn't matter, but I thought I should verify.

  • Stephen Petschulat
    Stephen Petschulat about 15 years
    That makes sense. When I was saving photos taken with the iPhone it would cause the odd problem, but when using PNGs of relatively simple vector images, there was no problem working with even quite large images.
  • rpetrich
    rpetrich about 15 years
    Can't agree with this more. If you have a crash, you can't just throw a bunch of things at it and hope something sticks