提问人:yxk 提问时间:10/13/2023 更新时间:10/13/2023 访问量:30
如何在UIListContentConfiguration中设置固定大小的UIImage?
How to set a fixed size UIImage in UIListContentConfiguration?
问:
我想要单元格中固定大小的图像,这是我在单元格中的代码:
var content = defaultContentConfiguration()
content.image = img
content.imageProperties.reservedLayoutSize = CGSize(width: 80, height: 80)
self.contentConfiguration = content
reservedLayoutSize 不起作用,有没有办法做到这一点?
答:
0赞
Chandaboy
10/13/2023
#1
尝试使用此代码来设置图像大小
// Create a UIListContentConfiguration for the cell
var contentConfig = cell.defaultContentConfiguration()
// Create a UIImage with a fixed size (e.g., 32x32 points)
let image = UIImage(named: "your_image_name")
let imageSize = CGSize(width: 32, height: 32)
let imageRenderer = UIGraphicsImageRenderer(size: imageSize)
let resizedImage = imageRenderer.image { _ in
image?.draw(in: CGRect(origin: .zero, size: imageSize))
}
// Set the image in the configuration
contentConfig.image = UIImage(systemName: "exmpleImage")
// Set any other properties in the configuration
contentConfig.text = myData[indexPath.row]
// Apply the configuration to the cell
cell.contentConfiguration = contentConfig
通过使用 UIGraphicsImageRenderer 创建具有所需大小的新图像,可以查看 UIImage 的代码大小。
评论