【问题标题】:Can I fetch PHAssets in a background thread?我可以在后台线程中获取 PHAsset 吗?
【发布时间】:2017-11-17 16:41:11
【问题描述】:

我想对用户的照片库做一些工作。由于图书馆可能很大,我想在后台进行。我想知道执行资产提取(如PHAsset.fetchAssets)并在后台处理它们是否安全?

我现在只需要资产元数据。

这样的东西安全吗:

class ViewController: UIViewController {

    var cachedResult = [Any]()

    func doBackgroundCalculationsOnPhotos(completionHandler: ([Any]) -> ()) {
        DispatchQueue.global(qos: .userInitiated).async {
            let photos = PHAsset.fetchAssets(with: .image, options: nil)
            var result = [Any]()

            photos.enumerateObjects({ asset, _, _ in  
                result.append(calculateSomething(asset))
            })

            DispatchQueue.main.async {
                self.cachedResult = result
                completionHandler(result)
            }
        }
    }
}

是否有任何参考文档可以让我了解照片框架和后台访问?

【问题讨论】:

  • 我也有同样的问题。

标签: ios swift multithreading photosframework


【解决方案1】:

是的,获取可能需要一些时间,因此最好在后台执行此操作,因为 fetchAssets(with:options:) 方法是同步的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    相关资源
    最近更新 更多