2015-11-29 55 views
1

我需要真的,真的帮助获取图像从当地的迅速

我将图片保存到DocumentDirectory如何利用这个图像,并使其到的UIImageView?

照片网址:

文件:///用户/ ZOOP /库/开发商/ CoreSimulator /设备/ 3E9FA5C0-3III-41D3-A6D7-A25FF3424351 /数据/集装箱/数据/应用/ 7C4D9316-5EB7- 4A70-82DC-E76C654EA201 /文档/ profileImage.png

回答

3

尝试类似的东西:

然后你就可以把imageUIImageView

其他选项(利奥Dabus在评论波纹管提到):

let fileName = "profileImage.png" 
let fileURL = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first!).URLByAppendingPathComponent(fileName) 
if let imageData = NSData(contentsOfURL: fileURL) { 
    let image = UIImage(data: imageData) // Here you can attach image to UIImageView 
} 
+0

它可能工作,但你不应该硬编码你的文件路径 –

+0

是的我知道,但在这里我展示了解决这个问题最简单的方法。 –

+2

最简单的方法是使用URLByAppendingPathComponent。顺便说一句你应该开始习惯NSURL而不是常规字符串 –

0

可以使用的NSFileManager的方法URLsForDirectory获取文档目录的URL和URLByAppendingPathComponent的文件名附加到原始地址:

if let fileURL = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first?.URLByAppendingPathComponent("profileImage.png"), 
    // get the data from the resulting url 
    let imageData = NSData(contentsOfURL: fileURL), 
    // initialise your image object with the image data 
    let image = UIImage(data: imageData) { 
    print(image.size) 
}