最佳答案
替换
cell.imageView!.image = image
与
cell.imageView?.image = image.scaleImage(toSize: CGSize(width: 40, height: 40))
将此 UIImageView
扩展添加到项目中。
extension UIImage {
func scaleImage(toSize newSize: CGSize) -> UIImage? {
var newImage: UIImage?
let newRect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height).integral
UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
if let context = UIGraphicsGetCurrentContext(), let cgImage = self.cgImage {
context.interpolationQuality = .high
let flipVertical = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: newSize.height)
context.concatenate(flipVertical)
context.draw(cgImage, in: newRect)
if let img = context.makeImage() {
newImage = UIImage(cgImage: img)
}
UIGraphicsEndImageContext()
}
return newImage
}
}
原因 您的代码中的错误是 UITableViewCell
正在根据其中的图像大小为 UIImageView
分配各种大小。此外,我已将图像调整为特定大小,使其适合 UITableViewCell
中的 imageView 内容。
注意: 请不要发布代码的屏幕截图,因为它不会帮助其他人从问题中复制它并且也会吸引 -ve 投票。但是,您可以发布 XIB、 Storyboard和模拟错误的屏幕截图。
关于ios - 更改 UITableViewCell 中 UIImageView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53412743/