相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get an error when trying to get all the photos from PHAssetCollection.fetchAssetCollections

Ask Question

I want to get all the photos of my custom album. but instead what I get is the below error.

My Code

let collections:PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)

Error i get

"Error returned from daemon: Error Domain=com.apple.accounts Code=7 "(null)""

Any ideas on how to fix this?

add first Privacy - Photo Library Usage Descriptionkey in your info.plist and then try to run this code – Divyesh Gondaliya Sep 5, 2019 at 10:10 I'm getting this error printed in the terminal but the PHAssets are being correctly retrieved. When I try to call the PHImageManager().requestImage in Xcode11 though, I need to specify options.deliveryMode = .highQualityFormat, while it used to work just fine with .fastFormat back in Xcode10. – marcelosalloum Oct 9, 2019 at 12:45 This doesn't actually stop you from fetching the assets. It could be a bug on Apple's side - file it on Feedback Assistant.app. – Pranav Kasetti Aug 2, 2020 at 10:07

Based on the comments, I'm not entirely sure what the issue is but I hope this code could provide some assistance. Using .album rather than .smartAlbum could also be part of the issue.

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)

Do everything with PHAsset on the main thread. I still get that error, but at least images are fetched.

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)
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

 
推荐文章