无法将 UIImage 添加到 UIImageView (Swift)
Can't add UIImage to UIImageView (Swift)
截至 2019 年 3 月,我正在尝试将 UIImage 添加到 Swift 中的 UIImageView。该图像的标题正确并保存在 iCloud Drive 中与 Swift 文件相同的目录中.我能够完美地将 UIImageView 添加到视图控制器,但图像没有显示在其中。我相信我有正确的文件名,因为我直接从取景器中复制了它。我想要做的是在查看器中有一个图像,该图像按纵横比缩放以适合查看器。
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
听起来是个问题。那不是图像需要的地方。它需要在您的资产目录或应用程序包中。
尝试将您的图片添加到 Images.xcassets
。
最佳做法是将图像添加到资产文件夹。通过添加您的 assets.xcassets 图像,它将被添加到您的 Bundle Resources 中。
如果您不想将它添加到 assets.xcassets 文件夹,请将图像添加为项目中的另一个项目。但是,请确保将图像添加到正确的目标。 (您可以在“文件检查器”>“目标成员资格”中查看)
- 您可以检查“构建阶段”>“复制捆绑资源”以确保将图像正确添加到捆绑包中。
// If you add image to assets.xcassets
let image = UIImage(named: "DiscoverButtonIcon")
// If you add image as a resource
let image = UIImage(named: "DiscoverButtonIcon.png")
// Set a break point here to debug your image.
- 相关调试post:Xcode debugging - displaying images
截至 2019 年 3 月,我正在尝试将 UIImage 添加到 Swift 中的 UIImageView。该图像的标题正确并保存在 iCloud Drive 中与 Swift 文件相同的目录中.我能够完美地将 UIImageView 添加到视图控制器,但图像没有显示在其中。我相信我有正确的文件名,因为我直接从取景器中复制了它。我想要做的是在查看器中有一个图像,该图像按纵横比缩放以适合查看器。
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
听起来是个问题。那不是图像需要的地方。它需要在您的资产目录或应用程序包中。
尝试将您的图片添加到 Images.xcassets
。
最佳做法是将图像添加到资产文件夹。通过添加您的 assets.xcassets 图像,它将被添加到您的 Bundle Resources 中。
如果您不想将它添加到 assets.xcassets 文件夹,请将图像添加为项目中的另一个项目。但是,请确保将图像添加到正确的目标。 (您可以在“文件检查器”>“目标成员资格”中查看)
- 您可以检查“构建阶段”>“复制捆绑资源”以确保将图像正确添加到捆绑包中。
// If you add image to assets.xcassets
let image = UIImage(named: "DiscoverButtonIcon")
// If you add image as a resource
let image = UIImage(named: "DiscoverButtonIcon.png")
// Set a break point here to debug your image.
- 相关调试post:Xcode debugging - displaying images