我在didSelectItemAt
中遇到UICollectionViewCell
的问题。我使用第三方框架从库中选择多张照片。然后传输所选资产并将其存储在PHAssets
数组中。我点击它们时使用一个小脚本来自定义预览图像。 (它基本上将图像扩展到全屏并放入,然后您可以向上或向下滑动以消除它)。问题不在于图片,而是在我的自定义UICollectionViewCell
中正确显示。问题是当我点击它们时。由于某种原因,它给出了错误的图像,我不确定原因。
var cameraPhotoUIImage: UIImage?
var assets = [PHAsset]()
lazy var assetsTurnedIntoImages =
{
return [UIImage]()
}()
lazy var imageManager = {
return PHCachingImageManager()
}()
extension PhotoVC : UICollectionViewDataSource, UICollectionViewDelegate
{
func setupCollectionView()
{
collectionView.dataSource = self
collectionView.delegate = self
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoPostCVCell", for: indexPath) as! PhotoPostCVCell
if let takenImage = cameraPhotoUIImage
{
cell.cellImage.image = takenImage
}
if assets.count > 0
{
let asset = assets[indexPath.row]
imageManager.requestImage(for: asset, targetSize: PHImageManagerMaximumSize, contentMode: .aspectFill, options: nil)
{ (image, info) in
cell.cellImage.contentMode = .scaleAspectFill
cell.cellImage.image = image!
self.assetsTurnedIntoImages.append(image!)
}
}
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
if assets.count > 0
{
return assets.count
}
else
{
return 1
}
}
// MARK: Preview Selected Image
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let postStoryboard = UIStoryboard(name: Storyboard_Post, bundle:nil)
if let destinationVC = postStoryboard.instantiateViewController(withIdentifier: "PreviewImageVC") as? PreviewImageVC
{
destinationVC.allowedDismissDirection = .both
destinationVC.maskType = .clear
if cameraPhotoUIImage != nil
{
destinationVC.transferedImageToPreview.image = cameraPhotoUIImage
destinationVC.showInteractive()
}
if assetsTurnedIntoImages.count > 0
{
print("Selected image to preview:",assetsTurnedIntoImages[indexPath.row] )
destinationVC.transferedImageToPreview.image = assetsTurnedIntoImages[indexPath.item]
destinationVC.showInteractive()
}
}
}
}
答案 0 :(得分:0)
您应该更新requestImage
方法。 requestImage方法看起来像异步。因此该请求不会等待块完成。如果您像这样更新您的块,它应该可以工作。
imageManager.requestImage(for: asset, targetSize: PHImageManagerMaximumSize, contentMode: .aspectFill, options: nil)
{ (image, info) in
DispatchQueue.main.async {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoPostCVCell", for: indexPath) as! PhotoPostCVCell
cell.cellImage.contentMode = .scaleAspectFill
cell.cellImage.image = image!
self.assetsTurnedIntoImages.append(image!)
}
}