1

I'm loading 100s 4k-8k png files(768*768 resolution), total less than 1mg.

Although I do convert them to UIImage and resize/combine images occasionaly,
I was surprised to see ipad device die because of memory warning due to the image loadings.

Is converting to UIImage takes up much more memory than actual byte size of the file?

Thank you.

eugene
  • 39,839
  • 68
  • 255
  • 489

2 Answers2

3

That's because png's are decompressed into memory, taking more memory. And each decompressed image will take up to 768*768*4 = 2.25 MByte of memory.

Nickolay Olshevsky
  • 13,706
  • 1
  • 34
  • 48
  • ah.. huge difference.. is there a way to tell (approximately) how much memory a given png will take up when decompressed? – eugene Jan 05 '11 at 23:47
  • 4
    yes - width x height x 4 gives you the size in bytes - the formula is the same for any decompressed image format because each pixel will be 4 bytes big. – deanWombourne Jan 05 '11 at 23:53
  • ah, doesn't matter whether compressed png file size was 4k or 40k? it'll be decompressed to width*height*4? also, whether png is pngcrushed(as apple's xcode does) or not doesn't matter when decompressed as far as file size goes? – eugene Jan 05 '11 at 23:54
  • 3
    No, because when it loads into memory it's uncompressed, period. You may be able to save some space if you know the PNG contains no alpha channel data - then you could potentially have it stored as RGB instead of RGBA. I've not tried that though. – Kendall Helmstetter Gelner Jan 06 '11 at 00:28
  • would original image size matter? if original image size were 320*320, it would be 320*320*4 even when it's displayed at 768*768 screen? – eugene Jan 06 '11 at 05:30
  • 1
    I think, that image itself will take only 320x320x4, however some additional memory will take up the view on which image is placed (i.e. UIImageView) for canvas. – Nickolay Olshevsky Jan 06 '11 at 08:09
0

You might want to consider how you load images if they aren't all for simultaneous display. There are lots of threads about this here and elsewhere, such as this thread.

UIImage imageNamed will cache the image (and sometimes Apple's caching is slightly buggy, not releasing properly) whilst UIImage imageWithData won't, so once no longer displayed, the memory will be released. There are advantages and disadvantages to both depending on your circumstances, so try to get a good understanding of the differences.

Community
  • 1
  • 1
Purpletoucan
  • 6,472
  • 2
  • 21
  • 28
  • Thanks, I'm actually loading them from sqlite, with UIImage imageWithData. Still, I'm loading them at once. As you said, I may need to reconsider how to keep/discard images – eugene Jan 05 '11 at 23:50