Khuong Khuong - 10 months ago 36
iOS Question

Can not get image from file

I have cache images in file like so:

enter image description here

I want to get these images, I have tried like this:

let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
let imagePath = documentsURL.absoluteString.stringByAppendingString("1.png")
let image = UIImage(contentsOfFile: imagePath)

the imagePath give me:


You see it gives me exactly the image path, but when I
, the image is

What I am doing wrong here. Please help me with this. Thanks.


Try this EDIT

let path = "~/1.png"
let expandedPath = path.stringByExpandingTildeInPath
let data = NSData(contentsOfURL: NSURL(string: expandedPath)!)

let image = UIImage(data: data!)

Hope this helps.