gpt4 book ai didi

ios - DidSelectItemAt Collection View 问题

转载 作者:行者123 更新时间:2023-11-30 12:12:29 28 4
gpt4 key购买 nike

我在 UICollectionViewCell 中使用 didSelectItemAt 时遇到问题。我正在使用第三方框架从库中选择多张照片。然后,所选 Assets 将被传输并存储在 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()
}

}
}
}

最佳答案

您应该更新 requestImage 方法。 requestImage 方法看起来像异步的。因此该请求不会等待 block 完成。如果你像这样更新你的 block ,它应该可以工作。

    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!)
}
}

关于ios - DidSelectItemAt Collection View 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45882572/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com