Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Image programmatically in swift

Tags:

ios

swift

I have an ImageView. This is the code I am using to add image to the ImageView

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

Now I have the image in my Project folder as you can see in the screenshotScreenshot

Still I am getting this error at run time

fatal error: unexpectedly found nil while unwrapping an Optional value

like image 769
Adam Young Avatar asked Apr 22 '15 06:04

Adam Young


2 Answers

Add your images to Images.xcassets

enter image description here

imageView.image = UIImage(named:"abc")
like image 159
Christos Chadjikyriacou Avatar answered Sep 21 '22 05:09

Christos Chadjikyriacou


What about this?

if let image = UIImage(named:"abc") {
   imageView?.image = image
}
like image 32
Konrad77 Avatar answered Sep 20 '22 05:09

Konrad77