PHAsset 无法获取
Posted
技术标签:
【中文标题】PHAsset 无法获取【英文标题】:PHAsset is failed to fetch 【发布时间】:2020-05-19 18:02:00 【问题描述】:我正在尝试从 ios 应用中的照片库中获取照片。我正在使用照片框架。 但是我在尝试这样做时遇到了错误,即使我只是在获得授权时才这样做:
[core] "从守护进程返回的错误:Error Domain=com.apple.accounts 代码=7 "(null)""
获取资产的代码如下:
func requestAuthorization()
phphotoLibrary.requestAuthorization [weak self] (status) in
guard let self = self else return
switch status
case .authorized:
self.getAllPhotos()
case .denied:
break
case .notDetermined:
break
case .restricted:
break
@unknown default:
break
private func getAllPhotos()
let assets: PHFetchResult = PHAsset.fetchAssets(with: .image, options: nil)
// rest of the codebase
【问题讨论】:
【参考方案1】:您好,这里是工作代码示例:
public enum FetchResults
case success(response: [PHAsset])
case error(error: Error)
public static func fetch(_ completion: @escaping (FetchResults) -> Void)
guard PHPhotoLibrary.authorizationStatus() == .authorized else
let error: NSError = NSError(domain: "PhotoLibrary Error", code: 1, userInfo: [NSLocalizedDescriptionKey: "No PhotoLibrary Access"])
completion(FetchResults.error(error: error))
return
DispatchQueue.global(qos: .userInitiated).async
let fetchResult = PHAsset.fetchAssets(with: .image, options: PHFetchOptions())
if fetchResult.count > 0
var assets = [PHAsset]()
fetchResult.enumerateObjects object, _, _ in
assets.insert(object, at: 0)
DispatchQueue.main.async
completion(FetchResults.success(response: assets))
【讨论】:
以上是关于PHAsset 无法获取的主要内容,如果未能解决你的问题,请参考以下文章
从 PHAsset 获取视频时如何检测视频是不是为横向/纵向?
无法将类型“[PHAsset]”的值分配给类型“UIImageView!”