我想获取我自定义相册中的所有照片。 但是我得到的是以下错误:
我的代码
let collections:PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
我遇到的错误
"从守护程序返回的错误:Error Domain=com.apple.accounts Code=7 "(null)""
您有什么想法可以解决这个问题吗?
我想获取我自定义相册中的所有照片。 但是我得到的是以下错误:
我的代码
let collections:PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
我遇到的错误
"从守护程序返回的错误:Error Domain=com.apple.accounts Code=7 "(null)""
您有什么想法可以解决这个问题吗?
private var fetchResult: PHFetchResult<PHAsset>!
func fetchOptions(_ predicate: NSPredicate?) -> PHFetchOptions {
let options = PHFetchOptions()
options.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: false) ]
options.predicate = predicate
return options
}
}
if let userLibraryCollection = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumUserLibrary, options: nil).firstObject {
self.fetchResult = PHAsset.fetchAssets(in: userLibraryCollection, options: fetchOptions(NSPredicate(format: "mediaType = \(PHAssetMediaType.image.rawValue)")))
} else {
self.fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions(nil))
}
private var fetchResult: PHFetchResult<PHAsset>!
func picker(_ picker: PHPickerViewController, didFinishPicking
results: [PHPickerResult]) {
self.dismiss(animated: true, completion: nil)
if let userLibraryCollection =
PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype:
.smartAlbumUserLibrary, options: nil).firstObject {
self.fetchResult = PHAsset.fetchAssets(in:
userLibraryCollection, options: fetchOptions(NSPredicate(format:
"mediaType = \(PHAssetMediaType.image.rawValue)")))
fetchResult.enumerateObjects { asset, index, stop in
PHAsset.getURL(ofPhotoWith: asset) { (url) in
if let imgurl = url{
print(imgurl)
}else {
print("error")
}
}
}
} else {
self.fetchResult = PHAsset.fetchAssets(with: .image, options:
fetchOptions(nil))
print(fetchResult.firstObject)
}
}
extension PHAsset {
static func getURL(ofPhotoWith mPhasset: PHAsset, completionHandler : @escaping ((_ responseURL : URL?) -> Void)) {
if mPhasset.mediaType == .image {
let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
return true
}
mPhasset.requestContentEditingInput(with: options, completionHandler: { (contentEditingInput, info) in
if let fullSizeImageUrl = contentEditingInput?.fullSizeImageURL {
completionHandler(fullSizeImageUrl)
} else {
completionHandler(nil)
}
})
} else if mPhasset.mediaType == .video {
let options: PHVideoRequestOptions = PHVideoRequestOptions()
options.version = .original
PHImageManager.default().requestAVAsset(forVideo: mPhasset, options: options, resultHandler: { (asset, audioMix, info) in
if let urlAsset = asset as? AVURLAsset {
let localVideoUrl = urlAsset.url
completionHandler(localVideoUrl)
} else {
completionHandler(nil)
}
})
}
}
}
在主线程上使用PHAsset
完成所有操作。我仍然会收到错误提示,但至少图片已经被获取。
let options = PHFetchOptions()
options.includeHiddenAssets = false
options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] // Newest first
options.predicate = NSPredicate(format: "mediaType = \(PHAssetMediaType.image.rawValue)") // Only images
DispatchQueue.main.async {
self.mediaAssets = PHAsset.fetchAssets(with: options)
}
PHAssets
被正确检索到了。然而,在 Xcode11 中尝试调用PHImageManager().requestImage
时,我需要指定options.deliveryMode = .highQualityFormat
,而在 Xcode10 中使用.fastFormat
就可以正常工作。 - marcelosalloum