尝试从 PHAssetCollection.fetchAssetCollections 获取所有照片时出错

Bjö*_*ner 45 swift photosframework

我想获取我的自定义相册的所有照片。但我得到的是以下错误。

我的代码

let collections:PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
Run Code Online (Sandbox Code Playgroud)

我得到的错误

“守护进程返回的错误:错误域=com.apple.accounts 代码=7“(空)”

有想法该怎么解决这个吗?

小智 1

根据评论,我不完全确定问题是什么,但我希望这段代码可以提供一些帮助。使用 .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))
    }
Run Code Online (Sandbox Code Playgroud)