如何在UIListContentConfiguration中设置固定大小的UIImage?

问题描述 投票:0回答:1

我想在单元格中使用固定大小的图像,这是我在单元格中的代码:

var content = defaultContentConfiguration()
content.image = img
content.imageProperties.reservedLayoutSize = CGSize(width: 80, height: 80)
self.contentConfiguration = content

reservedLayoutSize 不起作用,有办法做到这一点吗?

ios swift uitableview uikit uilistcontentconfiguration
1个回答
0
投票

尝试使用此代码来设置图像大小

 // 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 的代码大小。

© www.soinside.com 2019 - 2024. All rights reserved.