【发布时间】:2019-07-09 16:41:06
【问题描述】:
免责声明:我已经修改/正在修改我的导师从互联网上下载的项目。
这是我的家庭作业项目:屏幕上的 3 个幸运饼干,用户点击“找出我的幸运”按钮,随机选择三个 cookie 之一(使用 .randomElement() 属性)。将打开一个窗口,显示该随机 cookie 图像。但是,我无法将窗口内的图像设置为 .randomElement() 属性的结果
我已经尝试将 UIImage 转换为 UIImageView,反之亦然,但现在我束手无策。
这是正在形成的随机元素的全局类
class globalElements {
static let cookiesArray = [globalElements.numberOne,
globalElements.numberTwo, globalElements.numberThree]
static let randomlyAssignedCookie = cookiesArray.randomElement()
}
//And here is where the title error shows up:
let randomlyChosenFortune: UIImageView = {
let image = UIImageView()
image.translatesAutoresizingMaskIntoConstraints = false
image.clipsToBounds = true
image.contentMode = .scaleAspectFit
image.image = globalElements.randomlyAssignedCookie //(error: Cannot assign value of type 'UIImageView?' to type 'UIImage?')//
return image
}()
【问题讨论】:
-
你的 globalElements.randomlyAssignedCookie 的类型是什么?我认为这不是
UIImage:) 如果要将图像设置为UIImageView,则应将UIImage设置为imageView.image -
是的,你是对的,它是 UIImageView 类型的。你知道我怎么能解决这个问题吗?我束手无策,尝试了所有属性,甚至插值!
-
UIImageView 显示一个 UIImage。您已经说过您的图像是图像视图,但它可能是图像。将
Let image = UIImageView()更改为Let image = UIImage(),它可能会起作用。见Swift UIImageView reference
标签: ios swift uiimageview