ios - 我在 UIImageView 中添加图像但它不显示

标签 ios swift uiimageview uiimage

我在 UIImageView 中添加图像,但它不显示

let imageView = UIImageView(frame: CGRect(x: 100, y:20, width: 100, height: 100))
imageView.backgroundColor = UIColor.red
imageView.image = UIImage(named: "sample.jpg")
self.view.addSubview(imageView)

最佳答案

Note: Go to Assets.xcassets in your project. then create New Image Set with name sample. after that drag and drop sample.jpg into sample image set

let imageView = UIImageView(frame: CGRect(x: 100, y:20, width: 100, height: 100))
    imageView.backgroundColor = UIColor.red
    imageView.image = UIImage(named: "sample") // then use this
    self.view.addSubview(imageView)

关于ios - 我在 UIImageView 中添加图像但它不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46487204/

相关文章:

ios - 为什么我无法更新到最新的 iOS Cordova 平台?

xcode - 在containerView内的UIImageView上使用AutoLayout的Swift Xcode切面适应行为

ios - 如何将我的 UIImageView 内容保存到我的画廊? ( swift )

ios - 如何让用户从 Safari 复制图像并将其粘贴到我的应用程序的 UIImageView 中?

ios - DrawText on Image with text 调整到图像的大小

ios - 从类型 '(_, _, _) throws -> ()' 的抛出函数到非抛出函数类型的无效转换 '(URLResponse?, Data?, Error?) -> Void

ios - 交叉编译为静态库 (libgcrypt) 以便在 iOS 上使用

ios - 师生应用;移动设备之间的通信

ios - 如何在 xcode 中修复 "Failed to load model named datamodelname"

SwiftUI 将两个 subview 传递给 View