【发布时间】:2019-03-04 20:31:05
【问题描述】:
当我将本地数组中的图像设置到每个集合视图单元格中时,滚动很慢。这是因为在显示单元格时使用的是全尺寸的 UIImage。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCell", for: indexPath) as! PhotoCell
let image = images[indexPath.row]
cell.imageView.image = image
return cell
}
为了尝试解决这个问题,我编写了一个在运行时将图像大小调整为适当大小的方法。
private func resizeImages(images: [UIImage], size: CGSize) -> [UIImage?] {//...}
但是在viewDidLoad 中调整数组中所有图像的大小需要相当长的时间。
这是一个简单的相册应用程序,因此我希望避免使用任何活动或加载指示器。 如果可以访问从 Core Data 获取的图像数据,我如何在集合视图中设置图像以使其在滚动时不会滞后?
【问题讨论】:
标签: ios swift core-data uicollectionview uiimageview