views:

339

answers:

1

Hi,

I googled for the memory issue associated with UIImage and came across many threads talking about the problem but no real help or solution.

I know when we use -imageNamed: the object is being cached so it's better to use initWithData:. When we use drawRect: and UIGraphicsGetImageFromCurrentImageContext(), does the image goes to cache?

Also in the following code:

CGImageRef tmp = CGImageCreateWithImageInRect(imageToCrop.CGImage, clippedRect);    //pull the image from our cropped context    UIImage *cropped = [UIImage imageWithCGImage:tmp];//UIGraphicsGetImageFromCurrentImageContext();  
CGImageRelease(tmp);

Is caching takes place although there is no reference about this in the documentation? How do we release the memory that is getting consumed by the cache?

A: 

I'm not sure the answer to your first question, but as far as the second question in concerned, you should note that when you access the CGImage property of the UIImage, the entire image is loaded into memory. This is referenced in the documentation for the CGImage property.

If you are done with the image after use, you can simply call the release method on the UIImage in order to release the memory. If you want to keep it around but remove it from the cache, I can't think of any way other than to write the image to disk, release it and then create a new image using -initWithData:

Martin Gordon