之前在我们的项目中我们使用 PHAsset.fetchAssets(with:)
获取用户库中所有媒体的列表(返回为 PHFetchResult<PHAsset>
)。我们调用object(at:)
访问单个项目并且效果很好。
借助 iOS 14 及其新的隐私功能,用户可以选择仅允许访问其图库中选定的照片。问题是PHAsset.fetchAssets(with:)
仍然返回用户拥有的所有 Assets ,但只有应用有权访问的 Assets 才能用于检索实际的照片或视频。 count
PHFetchResult
的属性(property)在这种情况下也不需要。
有什么方法可以方便地仅请求具有访问权限的媒体项目,以便我立即获得正确的数量和 Assets ?
到目前为止,我发现的唯一示例是枚举 Assets 并为每个 Assets 请求相应的图像,并手动过滤掉无法访问的 Assets 。
let assets = PHAsset.fetchAssets(with: fetchOptions)
print(assets.count) // prints 6 and equals all items in photo library
var assetsWithGrantedAccess: [UIImage] = []
let filteredImagesQueue = DispatchQueue(label: "filter.images.queue")
let dispatchGroup = DispatchGroup()
assets.enumerateObjects { (asset, _, _) in
dispatchGroup.enter()
PHImageManager.default().requestImage(for: asset, targetSize: size, contentMode: .aspectFit, options: imageRequestOptions) { (image, _) in
defer { dispatchGroup.leave() }
guard let image = image else {
return
}
filteredImagesQueue.async {
assetsWithGrantedAccess.append(image)
}
}
}
dispatchGroup.notify(queue: filteredImagesQueue) {
print(assetsWithGrantedAccess.count) // prints 2 and equals number of items with granted access
}
最佳答案
这似乎只发生在模拟器中,设备上的 PHAsset.fetchAssets(with:)
仅返回具有授予访问权限的媒体项目。
关于ios - 如何在 iOS 14 上仅获取具有授予访问权限的 Assets ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63946227/