获取 swift 中的屏幕截图(相册)计数
Get screenshot (album) count in swift
我是 Swift 的新手,我正在尝试使用 fetchAssetCollections
制作一个简单 returns 截图相册中的照片数量的功能
我有
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
}
})
注意:使用此代码是Swift3
在 Swift 4 中,有一种非常快速的方法可以只获取 PHAssets
的屏幕截图,而不会造成混乱 if else
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)
我是 Swift 的新手,我正在尝试使用 fetchAssetCollections
制作一个简单 returns 截图相册中的照片数量的功能我有
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
}
})
注意:使用此代码是Swift3
在 Swift 4 中,有一种非常快速的方法可以只获取 PHAssets
的屏幕截图,而不会造成混乱 if else
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)