UIImage using contentsOfFile
Asked Answered
D

3

16

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

Disease answered 31/3, 2015 at 19:7 Comment(5)
Why it is so necessary to store images in images.xcassets? Seems like in your case there are more disadvantages than advantages. I would rather move images to normal folder and use pathForResource("foo", ofType: "png") method.Mair
So I can just create a separate folder in the file-system inside the app directory to use pathForResource()? Or do I have to create a group in XCode?Disease
You can create group, and add image there. Be sure, that image is copied to Copy Bundle Resources. Afterwards just write: var bundlePath = NSBundle.mainBundle().pathForResource("imageName", ofType: "jpg") var image = UIImage(contentsOfFile: bundlePath!)Mair
Thanks. If you put your comment as an answer, then I can accept it!Disease
"Why it is so necessary to store images in images.xcassets?" Because the .xcassets benefits from app thinning whereas the bundle solution doesn't.Crannog
M
39

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!) 
Mair answered 1/4, 2015 at 18:36 Comment(1)
Before using this version, the app starts with less memory (35mb) and increases over time (up to 150mb until all images were loaded). Now it starts instantly with 80mb but stays at the level. Any ideas for this behaviour?Timeout
P
1
import Foundation
import UIKit

enum UIImageType: String {
    case PNG = "png"
    case JPG = "jpg"
    case JPEG = "jpeg"
}

extension UIImage {

    convenience init?(contentsOfFile name: String, ofType: UIImageType) {
        guard let bundlePath = Bundle.main.path(forResource: name, ofType: ofType.rawValue) else {
            return nil
        }
        self.init(contentsOfFile: bundlePath)!
    }

}

Use:

let imageView.image = UIImage(contentsOfFile: "Background", ofType: .JPG)
Permafrost answered 20/12, 2018 at 6:24 Comment(1)
self.init(contentsOfFile: bundlePath)! unwrapping has no effect since it's a failable initializerAnesthetize
I
0

When you implement array of images from the images group resource,you can load each images(lets say b1,b2,b3,b4,b5) as

var imageIndex = 0
lazy var imageList = ["b1","b2","b3","b4","b5"]
let imagePath = Bundle.main.path(forResource: imageList[imageIndex], ofType: "jpg")
imageview.image = UIImage(contentsOfFile: imagePath!)
Iman answered 7/2, 2018 at 11:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.