Ios 获取swift中的屏幕截图(相册)计数
我是Swift新手,正在尝试使用fetchAssetCollections创建一个函数,该函数只返回屏幕截图相册中的照片数 我有Ios 获取swift中的屏幕截图(相册)计数,ios,swift,phassetcollection,Ios,Swift,Phassetcollection,我是Swift新手,正在尝试使用fetchAssetCollections创建一个函数,该函数只返回屏幕截图相册中的照片数 我有 func getNumScreenshots() -> Int { let collection:PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype:.smartAlbumScreenshots, options: nil)
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
}
})
let albumsPhoto:PHFetchResult=PHAssetCollection.fetchAssetCollections(with:.smartAlbum,子类型:.albumRegular,选项:nil)
枚举对象({(集合、索引、对象)在
如果collection.localizedTitle==“屏幕截图”{
让photoInAlbum=PHAsset.fetchAssets(in:collection,options:nil)
打印(photoInAlbum.count)//截图相册计数
}
})
注意:使用此代码是Swift 3在Swift 4中,有一种非常快速的方法可以获取只包含屏幕截图的
PHAssets
,而不会出现混乱的,否则
s:
Swift 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)
您可以使用SmartAlbum非常感谢这正是我要找的!好的,祝你好运!!您可以直接使用NSPredicate筛选照片。