swift - 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)
}
解决方案
来自 文档
默认情况下,此方法异步执行。如果您从后台线程调用它,您可以将 options 参数的 isSynchronous 属性更改为 true 以阻塞调用线程,直到请求的图像准备好或发生错误,此时 Photos 会调用您的结果处理程序。
所以
performSegue(withIdentifier: "segue099", sender: self)
在获取导致它传递旧图像的图像之前运行,因此或者true
将isSynchronous
segue 放在回调中
推荐阅读
- android - 为 IOS 和 Android 反应原生不同的包名称
- netty - grpc异步请求不等待结果
- loops - 给定一个整数列表和一个整数 k,返回 k 是否可以构建为该列表中任何数字的总和或它们的乘积
- influxdb - InfluxQL 到带有重采样和分组的新任务 FLUX
- c++ - 用户定义的容器不适用于 std::ranges
- google-apps-script - 您如何获取 GAS 项目中各个脚本的 ID?
- java - CloudFunction 作为 gradle 子项目
- javascript - 为什么等待服务器响应后不执行代码行
- python - 'TypeError:在尝试模拟异常时不允许捕获不从 BaseException 继承的类'
- python - 如何在 python 中调用使用命令行界面的 Web 服务?