[英]Resizing UIImage to fit table cell ImageView
I have images of the size 176 points by 176 points. 我的图像尺寸为176点乘176点。 I am trying to resize them so that they fit into a tableViewCell's ImageView.
我正在尝试调整它们的大小,以使它们适合tableViewCell的ImageView。 There are a few problems that I am coming across.
我遇到了一些问题。
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!
}
}
I am looking for information on how big the UIImageView is and how to best resize an image into it. 我正在寻找有关UIImageView的大小以及如何最佳地将图像调整到其中的信息。 I really don't want to create another set of assets (I have a lot of images), and I don't think I should have to.
我真的不想创建另一组资产(我有很多图像),我也不认为必须这样做。
Try changing the contentMode
of the cell. 尝试更改单元格的
contentMode
。
cell.imageView?.contentMode = .scaleAspectFit
Or, if that doesn't work, here's how to fix the issues of your resized images turning black: 或者,如果这样不起作用,请按照以下方法解决调整大小后的图像变黑的问题:
UIGraphicsBeginImageContextWithOptions(newSize, false, 0) // <-- changed opaque setting from true to false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.