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

SwiftPhotosframework

Swift Problem Overview


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?

Swift Solutions


Solution 1 - Swift

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))
    }

Solution 2 - Swift

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)
            }
        })
       }
    
    }
 }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBj&#246;rn LindnerView Question on Stackoverflow
Solution 1 - SwiftAlrightyRobView Answer on Stackoverflow
Solution 2 - SwiftSahil OmerView Answer on Stackoverflow