【发布时间】:2018-10-05 03:24:10
【问题描述】:
我有一个 UICollectionView,其中填充了直接来自照片库的用户图像。
如果用户在从库中选择图像后单击该图像,则主图像会切换到所选图像。
每个单元格都包含一个 UIImageView。
我尝试使用以下代码切换它们:
let cell = self.imageUICV.cellForItem(at: IndexPath(item: self.numOfCellSelected, section: 0)) as! AddImageCollectionViewCell
let mainCell = self.imageUICV.cellForItem(at: IndexPath(item: self.numOfCellSelected, section: 0)) as! AddImageCollectionViewCell
guard let mainImage = mainCell.itemImageUIImage.image?.cgImage?.copy() else {
return
}
let newMainImage = UIImage(cgImage: mainImage, scale: mainCell.itemImageUIImage.image!.scale, orientation: mainCell.itemImageUIImage.image!.imageOrientation)
guard let cellImage = cell.itemImageUIImage.image?.cgImage?.copy() else {
return
}
let newCellImage = UIImage(cgImage: cellImage, scale: cell.itemImageUIImage.image!.scale, orientation: cell.itemImageUIImage.image!.imageOrientation)
self.imagePlaceholderArray[self.numOfCellSelected] = newMainImage
self.imagePlaceholderArray[0] = newCellImage
self.imageUICV.reloadData()
但是,只有主图像发生了变化,而较小单元格中的图像保持不变。
【问题讨论】:
-
您的问题是什么?您发布的代码有什么问题?
-
该代码只改变主单元格图像,较小的单元格保持不变
-
你不能只保留一个带有
[UIImages]的数组和新选择的图像到该数组的顶部,然后只需重新加载你的集合视图。 -
代码有 [imagePlaceholderArray] 类型为 [UIImage],然后 .reloadData() 重新加载集合视图。问题仍然存在。
标签: swift uicollectionview uiimageview