我想要得到我的定制相册的所有照片。但相反,我得到的是下面的错误。
我的代码
let collections:PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)错误我得到
从守护进程返回的错误: Error Domain=com.apple.accounts Code=7“(Null)”
有什么办法解决这个问题吗?
发布于 2020-06-17 23:48:14
基于这些评论,我不完全确定问题是什么,但我希望这段代码能够提供一些帮助。使用.album而不是.smartAlbum也可能是问题的一部分。
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))
}发布于 2021-08-12 12:42:38
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)
}
})
}
}
}发布于 2022-08-17 05:49:44
在主线程上使用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)
}https://stackoverflow.com/questions/57803075
复制相似问题