How to set a fixed size UIImage in UIListContentConfiguration?

147 Views Asked by At

I want a fixed size image in cell, here is my code in cell:

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

reservedLayoutSize did not work, is there a way to do this?

1

There are 1 best solutions below

0
Chandaboy On

try to use this code in order to set image size

 // 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

as you can see the code size for the UIImage by creating a new image with the desired size using UIGraphicsImageRenderer.