ios - 无法将 UIImage 添加到 UIImageView (Swift)

标签 ios swift uiimageview uiimage

自 2019 年 3 月起,我正在尝试在 Swift 中将 UIImage 添加到 UIImageView。图像的标题正确,并保存在 iCloud Drive 中与 Swift 文件相同的目录中。我能够完美地将 UIImageView 添加到 View Controller ,但图像没有显示在其中。我相信我有正确的文件名,因为我直接从取景器中复制了它。我想要做的是在查看器中放置一张图像,该图像按纵横比缩放以适合查看器。

let discoverIcon = UIImage(named: "DiscoverButtonIcon.png")
    let discoverImageView = UIImageView(frame: CGRect(x: 200, y: 200, width: 100, height: 100))
    discoverImageView.image = discoverIcon
    discoverImageView.contentMode = .scaleAspectFit
    view.addSubview(discoverImageView)

这是我尝试做的,但 UIImage 不会显示。

最佳答案

暂时改变

UIImage(named: "DiscoverButtonIcon.png")

UIImage(named: "DiscoverButtonIcon.png")!

运行应用程序。如果它崩溃了,你的名字有误,或者图像根本不在你的应用程序包中。

is saved in the iCloud Drive in the same directory as the Swift file

这听起来像是问题所在。那不是图像需要的地方。它需要在您的 Assets 目录或应用程序包中。

关于ios - 无法将 UIImage 添加到 UIImageView (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55201671/

相关文章:

iphone - UITableViewCell 帮助中的 UITextField

ios - 如何更改 Swift 中表格 View 单元格中的默认删除按钮?

ios - Swift 3 中的 Wikitude 实例

objective-c - 在客户端处理分页 api

swift - POD 安装出现问题 : "` ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES"

arrays - iOS 如何找到浮点/整数值数组的值之间的最小差异

ios - 从另一个 UITableViewCell 类隐藏自定义 UITableViewCell

iphone - UIImagePickerController 保存到磁盘然后加载到 UIImageView

ios - 结合 2 张图像,工作但拉伸(stretch)

objective-c - 拖放 UIScrollView 上的 UIImageView