according to the apple documentation it is recommended to use xcassets for iOS7 applications and reference those images over imageNamed.
But as far as I'm aware, there were always problems with imageNamed and memory.
So I made a short test application - referencing images out of the xcassets catalogue with imageNamed and started the profiler ... the result was as expected. Once allocated memory wasn't released again, even after I removed the ImageView from superview and set it to nil.
I'm currently working on an iPad application with many large images and this strange imageView behavior leads to memory warnings.
But in my tests I wasn't able to access xcassets images over imageWithContentsOfFile.
So what is the best approach to work with large images on iOS7? Is there a way to access images from the xcassets catalogue in another (more performant) way? Or shouldn't I use xcassets at all so that I can work with imageWithContentsOfFile?
Thank you for your answers!
UPDATE: Cache eviction works fines (at least since iOS 8.3).
I decided to go with the "new Images.xcassets" from Apple, too. Things started to go bad, when I had about 350mb of images in the App and the App constantly crashed (on a Retina iPad; probably because of the size of the loaded images).
I have written a very simple test app where I load the images in three different types (watching the profiler):
imageNamed:
loaded from an asset: images never gets released and the app crashes (for me I could load 400 images, but it really depends on the image size)imageNamed:
(conventionally included to the project): The memory usage is high and once in a while (> 400 images) I see a call todidReceiveMemoryWarning:
, but the app is running fine.imageWithContentsOfFile([[NSBundle mainBundle] pathForResource:...)
: The memory usage is very low (<20mb) because the images are only loaded once at a time.I really would not blame the caching of the
imageNamed:
method for everything as caching is a good idea if you have to show your images again and again, but it is kind of sad that Apple did not implement it for the assets (or did not document it that it is not implemented). In my use-case, I will go for the non-cachingimageWithData
because the user won't see the images again.As my app is almost final and I really like the usage of the loading mechanism to find the right image automatically, I decided to wrap the usage:
Script:
(implementation not complete for all devices and fallback mechanisms!!)