我是新来的斯威夫特,我试图做的是简单地返回使用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
在斯威夫特4有以获取刚刚截图没有任何头痛和恼人的PHAssets
的if else
一个非常简单的方法:
斯威夫特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)