ios - 无法分配给属性 : 'size' is a get-only property Swift

标签 ios swift

我正在尝试从 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)
}

输出是 enter image description here

我是否错误地调整了它的大小?

最佳答案

只给帧大小就足够了。重要的一点是您需要设置图像的 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/

相关文章:

objective-c - ios 的 mapkit 中的 didupdateuserlocation 方法?

ios - 自 iOS6 以来,GLKTexture 未正确映射

iphone - 在 UIScrollView 中滚动 CoreText

ios - 在 Swift 2 中创建异步任务

ios - 滚动时重置表格 View 单元格信息

iphone - iPhone应用程序生命周期中的最后一个功能是什么

ios - 结构数组不在闭包外更新

ios - 处理URLSession时是否总是需要[Weak self]?

ios - Swift:如何将不同的 UITableViewCell 执行 Segue 到不同的 ViewController?

ios - 将 NSNotification 与 CoreData 结合使用