2015-11-29 56 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) 
}