PHAsset分页抓取

时间:2017-11-30 04:54:43

标签: ios swift phasset photokit

有没有办法通过分页加载PHAssets?以下是获取存储在我的设备上的照片的代码(检查手机上照片的可用性需要花费太多时间)。因此,我需要一种有效的方式来加载资产。

func getOption() -> PHFetchOptions {
    let options = PHFetchOptions()
    let sortOrder = [NSSortDescriptor(key: "modificationDate", ascending: false)]
    options.sortDescriptors = sortOrder
    return options
}

func fetchPhotosOnDevice(completion: @escaping ([PHAsset]) -> Void) {
    let options = getOption()
    options.predicate = NSPredicate(format: "mediaType = %i", PHAssetMediaType.image.rawValue)
    let fetchResults = PHAsset.fetchAssets(with: .image, options: options)

    DispatchQueue.global().async {
        var assets: [PHAsset] = []
        fetchResults.enumerateObjects({ (asset, idx, bool) in
            if let _ = asset.fullResolutionImage {
                assets.append(asset)
            }
        })
        completion(assets)
    }
}

0 个答案:

没有答案