Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImage using contentsOfFile

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 :) !

like image 784
rgamber Avatar asked Mar 31 '15 19:03

rgamber


People also ask

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

How do I find my UIImage path?

Once a UIImage is created, the image data is loaded into memory and no longer connected to the file on disk. As such, the file can be deleted or modified without consequence to the UIImage and there is no way of getting the source path from a UIImage.

What is a UIImage?

An object that manages image data in your app.

How do I display an image in Objective C?

I am sure you're more than aware, but you can display an image in 2 lines using a UIImageView: UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImage. png"]]; [self. view addSubview:imageView];


1 Answers

If you need to use pathForResource() for avoiding image caching, it is not possible to work with images.xcassets. In that case you need to create group in XCode, and add image there (be sure, that image is copied to Copy Bundle Resources). Afterwards just write:

Swift 5:

let bundlePath = Bundle.main.path(forResource: "imageName", ofType: "jpg")
let image = UIImage(contentsOfFile: bundlePath!)

Older Swift:

let bundlePath = NSBundle.mainBundle().pathForResource("imageName", ofType: "jpg") 
let image = UIImage(contentsOfFile: bundlePath!) 
like image 139
Kateryna Gridina Avatar answered Sep 22 '22 13:09

Kateryna Gridina