自 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/