Do I need to dispose UIImage
s and CGImage
s in MonoTouch application after I've finished working with them? What are the effects of disposing either of them?
Is it safe to dispose any of them if the image is still visible?
I suggest using a using
block for images like so:
using (var image = UIImage.FromFile ("demo.png"))
{
//do something with your image here
//such as passing it to an UIImageView.Image
}
When the object leaves that block it will get disposed automatically.
Alternatively you can call Dispose()
on the UIImage when you know you are done using it.
If you do not dispose of them and load a lot of images you will eventually run out of memory. Loading big images with the UIImage.FromBundle()
method is a bad idea as they are cached and you have no control over when the cache is disposed of.
If you dispose an UIImage
which is currently shown nothing bad will happen with the UIImageView
as it keeps its own reference to the image. So only the C# managed image is disposed of but not necessarily the Cocoatouch image.