依次获取PHAsset

问题描述

我正在尝试通过传递本地标识符数组来从照片库中获取资产。但是输出的顺序不同。我的应用程序要求我按顺序获取它们。这就是我的使用方式

let fetchedResults = PHAsset.fetchAssets(withLocalIdentifiers: videosinLibrary,options: nil)
fetchedResults.enumerateObjects { (asset:PHAsset,count:Int,stop:UnsafeMutablePointer<ObjCBool>) in

我们有什么办法可以按照我传递的本地标识符的顺序来获取它们。

解决方法

它是数据库,因此返回存储状态,但是您可以在本地进行操作:

let fetchedResults = PHAsset.fetchAssets(withLocalIdentifiers: videosInLibrary,options: nil)

var tmpCache = [String: PHAsset]()
fetchedResults.enumerateObjects { (asset:PHAsset,count:Int,stop:UnsafeMutablePointer<ObjCBool>) in
    tmpCache[asset.localIdentifier] = asset
}

let results = videosInLibrary.compactMap { tmpCache[$0] }