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

标签 ios swift

我有一个 ImageView。这是我用来将图像添加到 ImageView 的代码

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

现在我的项目文件夹中有了图像,您可以在屏幕截图中看到 Screenshot

我仍然在运行时遇到这个错误

fatal error: unexpectedly found nil while unwrapping an Optional value

最佳答案

将您的图像添加到 Images.xcassets

enter image description here

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

关于ios - 以编程方式快速加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29789748/

相关文章:

ios - Xcode UIView.init(帧 :) must be used from main thread only

ios - 在 iOS 上绘制圆角图像时遇到奇怪的锯齿

ios - 如何在 swift 3 的 tabor 中使用 2 种颜色的自定义图标?

xcode - 无法存档,但 "build for archive"仍然有效(Xcode 4)

ios - native iOS 应用程序中的 PayPal Express Checkout

xcode - 更改 ParentViewController 中 ChildViewController 中 UIPageControl 的点 - swift

ios - 即使用户关闭应用程序,如何保持用户登录?我正在使用 swift 3 和 Firebase

ios - 首次加载时 NSCache 不适用于所有图像

ios - 我已使用桥接 header 将 Objective-C 文件导入到 Swift 项目中,但出现错误 "Expected a type"

ios - 从 FSCalender -Swift 获取当前月份和年份