I am using
self.imageView.image = UIImage(named: "foo.png")
to select and load images in an UIIMageView
. I have the images in my app under the images.xcassets
. The problem is that this particular init
caches the image for reuse as per the official Apple documentation:
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.
My view allows cycling through images before selecting one, and so the memory footprint goes on increasing as I cycle through and never goes down even when I navigate back from that view.
So I am trying to use UIIMage(contentsOfFile: "path to the file")
which does not cache the image. Here I am having trouble programatically getting the path of the images which I have stored under images.xcassets.
I have tried using:
NSBundle.mainBundle().resourcePath
and NSBundle.mainBundle().pathForResource("foo", ofType: "png")
without luck. For the first one I get the resourcePath, but on accessing that through the terminal I do not see any image assets under it, and the second one I get nil
when I use it. Is there an easy way to to do this?
Also looked at several SO questions (like this, this and this) with no luck. Do I have to put my images somewhere else to be able to use the pathForResource()
? What is the right way to go about this?
It is hard to imagine that no one has encountered this scenario before :) !