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

前端 未结 1 716
南旧
南旧 2021-02-11 13:38

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 = PHAs         


        
1条回答
  •  余生分开走
    2021-02-11 14:22

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

    0 讨论(0)
提交回复
热议问题