4

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

我有

func getNumScreenshots() -> Int {

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

    return collection.count
}

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

4

2 回答 2

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

注意:使用此代码是 Swift 3

于 2017-02-13T01:58:20.200 回答
1

在 Swift 4 中,有一种非常快速的方法可以PHAssets只获取 ScreenShots,而不会出现乱七八糟if else的 s:

斯威夫特 4:

let sc_Fetch = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: **.smartAlbumScreenshots**, options: nil).firstObject!

let sc_Assets = PHAsset.fetchAssets(in: sc_Fetch , options: nil)

print("All ScreenShots Count : " , sc_Assets.count)
于 2019-02-06T18:20:31.420 回答