2015-04-22 58 views
4

我有一個ImageView。這是我使用的圖像添加到ImageView的以swogra編程式加載圖像

var imgstr = pf["ImageFileName"] as String 
image = UIImage(named: imgstr)! 
println(imgstr) //the output here is abc.png 
self.Imageview.image = image 

現在我有形象在我的項目文件夾,你可以在截圖Screenshot

不過我得到在運行時此錯誤看到代碼

fatal error: unexpectedly found nil while unwrapping an Optional value 
+0

你可以檢查圖像'abc.png'列在__Build階段 - >複製捆綁資源_? –

+0

可以self.ImageView爲零?你有沒有正確設置它?如果您使用的是Storyboard,是否正確設置了引用插口? – Nycen

回答

11

添加您的圖像Images.xcassets

enter image description here

imageView.image = UIImage(named:"abc") 
0

self.Imageview!.image = image 

,而不是

self.Imageview.image = image 
+0

仍然無法正常工作。 –

2

這個是什麼?

if let image = UIImage(named:"abc") { 
    imageView?.image = image 
}