在swift中获取截图(专辑)计数

rjv*_*nti 3 ios swift phassetcollection

我是Swift的新手,我正在尝试创建一个函数,只需使用fetchAssetCollections返回截屏相册中的照片数量

我有

func getNumScreenshots() -> Int {

    let collection:PHFetchResult =  
     PHAssetCollection.fetchAssetCollections(with: .album, subtype:.smartAlbumScreenshots, options: nil)

    return collection.count
}
Run Code Online (Sandbox Code Playgroud)

但是,这总是返回3,我不知道为什么(我的iPhone上有600个截图).

Sou*_*ean 6

你可以尝试这个:

let albumsPhoto:PHFetchResult<PHAssetCollection> = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .albumRegular, options: nil)

    albumsPhoto.enumerateObjects({(collection, index, object) in
        if collection.localizedTitle == "Screenshots"{
            let photoInAlbum = PHAsset.fetchAssets(in: collection, options: nil)
            print(photoInAlbum.count) //Screenshots albums count
        }
    })
Run Code Online (Sandbox Code Playgroud)

注意:使用此代码是Swift 3