获取截图(专辑)计数迅速
问题描述:
我是新来的斯威夫特,我试图做一个函数,简单地返回您的截图相册的照片数量使用fetchAssetCollections获取截图(专辑)计数迅速
我
func getNumScreenshots() -> Int {
let collection:PHFetchResult =
PHAssetCollection.fetchAssetCollections(with: .album, subtype:.smartAlbumScreenshots, options: nil)
return collection.count
}
然而,这总是返回3,我不知道为什么(我的iPhone上有600张截图)。
答
你可以用这个尝试:
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
}
})
注:使用此代码是斯威夫特3
可以使用smartAlbum –