views:

861

answers:

1

So here's the situation:

I have a CALayer that is the size of my screen, and I'm setting the contents property to a 2 Mb JPEG that's roughly 3500 x 2000 pixels in size with a resolution of 240ppi.

I'd expect there to be a slight overhead involved in using the CALayer, but my sample application (which only does exactly what's above) shows usage of about 33Mb RSIZE, 22Mb RPVT and 30Mb RSHRD. I've noticed that these numbers are much better when running the application as 64-bit than they are running as a 32-bit process.

I'm doing everything I can think of in the real application that this example comes from, including resampling my CGImageRefs to only be the size of the layer, but this seems extraneous to me - shouldn't it be simpler?

Has anyone come across good methods to reduce the amount of memory CALayers and CGImageRefs use?

+3  A: 

First, you're going to run into problems with an image that size in a plain CALayer, because you may hit the texture size limit of 2048 x 2048 (depending on your graphics card). Applications like this are what CATiledLayer is designed for. Bill Dudney has some code examples on his blog (a large PDF), as well as with the code that accompanies his book.

It isn't surprising to me that such a large image would take so much memory, given that it will be stored as an uncompressed bitmap in your CGImage. Aside from scaling your image to the resolution you need, and tiling it with CATiledLayer, I can't think of much. Are you releasing the CGImageRef once you've assigned it to the contents of the CAlayer? You won't need to hang onto it at that point.

Brad Larson
The only reason I hand on to the CGImageRef after it is in the contents property of my layer is to save some time - I figured the trade off of loading from disk again very quickly vs. keeping the image around would be worth it. I'll try releasing it though and see what impact that has - thanks!
Tony Arnold
Releasing it made a small difference, but keeping these images in memory seems to be the main culprit. Is it possible to keep the compressed versions in memory using CGImageRefs and decompress on the fly? Surely that would be faster than reading in from disk again...
Tony Arnold