ios 以编程方式快速加载图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29789748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:44:00  来源:igfitidea点击:

Load Image programmatically in swift

iosswift

提问by Adam Young

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

我有一个 ImageView。这是我用来将图像添加到 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

致命错误:在展开可选值时意外发现 nil

回答by Christos Chadjikyriacou

Add your images to Images.xcassets

将您的图像添加到 Images.xcassets

enter image description here

在此处输入图片说明

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

回答by Konrad77

What about this?

那这个呢?

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

回答by x4h1d

put

self.Imageview!.image = image

instead of

代替

self.Imageview.image = image