5

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

2
  • Can you check the image abc.png is listing under Build Phases -> Copy bundle resources ? Commented Apr 22, 2015 at 6:59
  • Could self.ImageView be nil? Did you properly set it up? If you're using Storyboard, is the referencing outlet properly set? Commented Apr 22, 2015 at 7:02

3 Answers 3

17

Add your images to Images.xcassets

enter image description here

imageView.image = UIImage(named:"abc")
Sign up to request clarification or add additional context in comments.

Comments

3

What about this?

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

Comments

0

put

self.Imageview!.image = image

instead of

self.Imageview.image = image

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.