gpt4 book ai didi

ios - 调整 UIImage 大小以适应表格单元格 ImageView

转载 作者:行者123 更新时间:2023-11-28 16:02:27 24 4
gpt4 key购买 nike

我有大小为 176 点 x 176 点的图像。我正在尝试调整它们的大小,以便它们适合 tableViewCell 的 ImageView。我遇到了一些问题。

问题

  • 我不知道 tableViewCell 中的 ImageView 实际有多大。
  • 如果我只是简单地添加图片而不调整它的大小,它会太清晰以至于丢失细节: Sharp Image
  • 如果我在 UIImage 上使用这个扩展(如下),那么 UIImage 的透明部分会变成黑色,这不是我想要的:

Black Image

extension UIImage {
func resizeImageWithBounds(bounds: CGSize) -> UIImage {
let horizontalRatio = bounds.width/size.width
let verticalRatio = bounds.height/size.height
let ratio = max(horizontalRatio, verticalRatio)
let newSize = CGSize(width: size.width * ratio, height: size.height * ratio)
UIGraphicsBeginImageContextWithOptions(newSize, true, 0)
draw(in: CGRect(origin: CGPoint.zero, size: newSize))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
}

我正在寻找有关 UIImageView 有多大以及如何最好地将图像调整到其中的信息。我真的不想创建另一组 Assets (我有很多图像),我认为我不必这样做。

最佳答案

尝试更改单元格的 contentMode

cell.imageView?.contentMode = .scaleAspectFit

或者,如果这不起作用,请按照以下方法解决调整大小后的图像变黑的问题:

UIGraphicsBeginImageContextWithOptions(newSize, false, 0) // <-- changed opaque setting from true to false

关于ios - 调整 UIImage 大小以适应表格单元格 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40813473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com