我正在尝试从 URL 检索图像,但我不知道为什么我的图像如此之大,我尝试调整框架大小,但没有用。我正在考虑调整图像大小,但它显示
“无法分配给属性:‘size’是一个只获取属性”
我使用的调整大小的代码是:
if let ImageUrl = message.imageUrlLink {
receivedimg.frame.size = CGSize(width: 150, height: 150)
receivedimg.image?.size = CGSize(width: 150, height: 150) // error shown here
self.receivedimg.loadImageUsingCacheWithUrlString(ImageUrl)
}
我是否错误地调整了它的大小?
最佳答案
只给帧大小就足够了。重要的一点是您需要设置图像的 contentMode。当你给
if let ImageUrl = message.imageUrlLink {
receivedimg.frame.size = CGSize(width: 150, height: 150)
receivedimg.image?.contentMode = UIViewContentMode.scaleAspectFit
self.receivedimg.loadImageUsingCacheWithUrlString(ImageUrl)
}
来自 Apple 文档;
scaleAspectFit: The option to scale the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view’s bounds is transparent.
您可以查看下面的 Apple 文档以获取有关 contentModes 的更多信息:
https://developer.apple.com/documentation/uikit/uiviewcontentmode
关于ios - 无法分配给属性 : 'size' is a get-only property Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50502871/