Khuong Khuong - 1 year ago 80
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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download