如何为 UIListContentConfiguration 提供视网膜大小的图像?

How to provide retina sized image for UIListContentConfiguration?

使用UIListContentConfiguration时,如何提供视网膜大小的图像?我看到单元格的图像视图的大小是根据您提供的图像的大小而定的。想象一下,我想在 28x28pt 的正方形(类似于照片应用侧边栏中的相册)中显示从某些 URL 中获取的缩略图,因此图像需要为 56x56px 以防止在 2x iPad 上出现像素化。但这会导致单元格中的图像视图为 44x44pt,因为它似乎试图将其设为 56x56pt,但单元格不够高,所以它在单元格高度处达到最大值。

let rowRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, SidebarItem> { (cell, indexPath, item) in
    let thumbnailSize = CGSize(width: 28 * UIScreen.main.scale, height: 28 * UIScreen.main.scale)

    var contentConfiguration = UIListContentConfiguration.sidebarCell()
    contentConfiguration.text = item.title
    contentConfiguration.image = downloadedImage.preparingThumbnail(of: thumbnailSize)
    cell.contentConfiguration = contentConfiguration

    // downloadedImage is an image downloaded from some url, it could be any size like 100x100 for example
    // need to resize it to create a 56x56px image on a 2x screen, intended to be displayed in a 28x28pt image view
}

我发现图像的 scale 需要与显示比例相匹配,图像视图才能在单元格中正确调整大小。如果你有一张 56x56px 1x 图像,你想在 2x 显示器上以 28x28pt 显示,你可以创建一个新图像来设置比例,如下所示:

let thumbnailImage = downloadedImage.preparingThumbnail(of: thumbnailSize) //56x56px @ 1x displays in 56x56pt image view
let scaledThumbnailImage = UIImage(cgImage: thumbnailImage.cgImage!, scale: UIScreen.main.scale, orientation: thumbnailImage.imageOrientation) //56x56px @2x displays in 28x28pt image view