Programming in iOS: imageNamed vs imageWithContentsOfFile
One easy way to load an image by name (from assets or from nib) is to use imageNamed method from UIImage. Without reading the UIImage reference, it may not be immediately understood that this also places the image into a cache. For single-use images, this may lead to unnecessary memory growth, especially for large images such as backdrops that the user may update several times. Here I’m loading an image named, “MarbleWhite” from a PNG file.
Checking the “Quick Help” in Xcode or the reference for “+ (UIImage *)imageNamed:(NSString *)name” reveals the cache warning:
This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method locates and loads the image data from disk or asset catelog, and then returns the resulting object.
The reference offers an alternative:
If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.
If using our own cache for memory management, or for loading single-use image such as a backdrop, better to load from a file as suggested.
It always pays to check the programming documentation.