Rafał Sroka
Rafał Sroka

Reputation: 40028

JPG image memory footprint in UIImageView

I know that PNG images are compressed and when loaded as UIImages to a UIImageView they occupy much more in the memory then the original file. Does this also apply to JPG images?

Upvotes: 1

Views: 555

Answers (1)

Rob Keniger
Rob Keniger

Reputation: 46028

Short answer: Yes.

Long answer: JPEG is a lossy compression scheme and can compress images such that they are orders of magnitude smaller than the original bitmap. PNG is a lossless compression scheme and therefore is not able to achieve nearly as good compression ratios as JPEG.

Because of this, a very small JPEG file can balloon to huge sizes when decompressed, much larger than any comparable PNG file.

However, what you need to be aware of is that once an image is loaded into memory as a bitmap, it will always consume a predictable amount of memory, no matter what type of file it is loaded from.

That's because bitmap images use a precise amount of memory per pixel (in fact, unsurprisingly, for a standard 32-bit image, it's 32 bits per pixel). So you can easily calculate the memory required for your image (assuming it's a 32-bit image) by multiplying the width in pixels by the height in pixels and multiplying that by 4 (which is 32 / 8 bits per byte). That will tell you how many bytes are required to store your image's bitmap data in RAM.

So a 640 x 480 pixel, 32-bit image uses: 640 * 480 * 4 = 1228800 bytes, which is just over one megabyte. The UIImage object that uses this bitmap as a backing store will add a small amount of overhead to that.

It's worth noting that the Apple 27-inch display used on the iMac etc has a resolution of 2560 x 1440. That works out to 14 megabytes of RAM required just to hold the bitmap image of the desktop!

Upvotes: 6

Related Questions