Adam Young
Adam Young

Reputation: 1331

Load Image programmatically in 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

Upvotes: 5

Views: 25548

Answers (3)

Konrad77
Konrad77

Reputation: 2535

What about this?

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

Upvotes: 3

Christos Chadjikyriacou
Christos Chadjikyriacou

Reputation: 3759

Add your images to Images.xcassets

enter image description here

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

Upvotes: 17

x4h1d
x4h1d

Reputation: 6092

put

self.Imageview!.image = image

instead of

self.Imageview.image = image

Upvotes: 0

Related Questions