首页 > 解决方案 > CollectionViewCell 发送上一张图片

问题描述

我正在发送从 collectionView 中选择的图像。问题是应用程序总是发送以前选择的图像。indexPath 有什么问题?

这是 didSelectItemAt 函数:

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let options = PHImageRequestOptions()
    options.deliveryMode = .highQualityFormat
    options.isNetworkAccessAllowed = true
    options.isSynchronous = true

    PHImageManager.default().requestImage(for: fetchResult.object(at: indexPath.item), targetSize: PHImageManagerMaximumSize, contentMode: .aspectFit, options: options, resultHandler: { image, error in   
        guard let image = image else { return }          
        self.image = image

    })

    performSegue(withIdentifier: "segue099", sender: self)

}

标签: swiftcollectionview

解决方案


来自 文档

默认情况下,此方法异步执行。如果您从后台线程调用它,您可以将 options 参数的 isSynchronous 属性更改为 true 以阻塞调用线程,直到请求的图像准备好或发生错误,此时 Photos 会调用您的结果处理程序。

所以

performSegue(withIdentifier: "segue099", sender: self)

在获取导致它传递旧图像的图像之前运行,因此或者trueisSynchronoussegue 放在回调中


推荐阅读