Using CGContextDrawTiledImage at different zooms causes massive memory growth

Posted by Jacques on Stack Overflow See other posts from Stack Overflow or by Jacques
Published on 2010-10-16T23:42:19Z Indexed on 2011/01/08 13:53 UTC
Read the original article Hit count: 222

Filed under:
|
|

I'm working on app an where there's a view in a zoomable UIScrollView. When the user zooms in or out, I redraw the view that's in the UIScrollView to be nice and sharp. That view has a background image that I draw with CGContextDrawTiledImage.

I noticed that memory usage grows every time I switch to a new zoom level. It looks like CGContextDrawTiledImage keeps a cache somewhere of the image scaled to different sizes. So, If I go from 1.0 to 1.1x zoom, memory use grows. Going back to 1.0 doesn't cause it to grow, but then going to 1.05 and then 1.2 causes it to grow twice. Back to 1.1 and no growth. Of course, the zoom level is under user control so I don't have control over how many zoom levels happen. Right now my background image is kind of massive (512x512), so this causes memory usage to grow very quickly. It doesn't show up as a memory leak in Instruments, just additional allocations that never get freed.

I've tried to find a way to free the cache that appears to be being created, but no luck. It doesn't seem to respond to low memory warnings, for example.

I also tried setting the view's backgroundColor to a UIColor created with colorWithPatternImage, but that doesn't work because I'm doing the scaling by changing the graphics context's CTM, not by setting the view's transform.

Any ideas on how to keep memory usage from blowing up?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about cocoa-touch