I am having a problem with CoreGraphics / CoreAnimation on iPhone. To better explain how the problem manifests, I will walk you through my current setup and illustrate the code where necessary.
I am trying to draw a bunch of preloaded images in a UIView CALayer , but whenever the image is displayed, the memory consumption of the application increases and the memory is not restored whenever the image changes.
Images are UIImage by reading them through UIImage objects, rendering them into a Bitmap context, and extracting CGImageRef from this context. The purpose of this is to unzip and scale the images so that these operations are not performed at every draw. A similar recommendation can be found in Apple Q & A on this subject (search for CGContextDrawImage performance, if you're interested). The context is set to 8 bits per component and pre-multiplied alpha.
After the images are unpacked into a raster image, they are saved in NSArray and later assigned (not saved) to the user-defined subclass of UIView that executes the drawing. I tried various approaches to drawing images, and by far the fastest method directly sets the view CALayer contents property. Other methods, such as drawLayer:inContext: and drawRect: have different effects on frame rates, but they all have the same memory behavior.
The problem is that after changing the contents property, I see a surge in the memory in the tools and this memory does not fall even after the image is no longer displayed. Object selections remain constant, so I assume that CoreAnimation creates some implicit cache to speed up drawing. However, as I said, this cache is not released when necessary, and a gradual build-up leads to failure in just a couple of minutes of work.
The contents property saves the object, and I do not explicitly release it, because I want the original images to remain in memory for the duration of the application; Also, a high retention rate would not account for the bursts of memory that I see.
When I look at the stack, I see that CoreAnimation makes calls to functions like CA::Render::copy_image , which makes me believe that it duplicates the contents of the layer somewhere out of reach. I believe that there is a good reason for this, but I donβt know how and when to clear this show stop error at present.
So can anyone with confusing CA knowledge please explain to me if I am doing something wrong and how I can get around this behavior.
Thanks.