简体   繁体   中英

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 screenshot 截图

Still I am getting this error at run time

fatal error: unexpectedly found nil while unwrapping an Optional value

Add your images to Images.xcassets

在此输入图像描述

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

What about this?

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

put

self.Imageview!.image = image

instead of

self.Imageview.image = image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM