2017-02-13 53 views
2

我是新来的斯威夫特,我试图做一个函数,简单地返回您的截图相册的照片数量使用fetchAssetCollections获取截图(专辑)计数迅速

func getNumScreenshots() -> Int { 

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

    return collection.count 
} 

然而,这总是返回3,我不知道为什么(我的iPhone上有600张截图)。

+0

可以使用smartAlbum –

回答

4

你可以用这个尝试:

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

+0

谢谢你这么多这正是我一直在寻找! – rjvalenti

+0

好祝你好运! –

+0

您可以直接使用NSPredicate过滤照片。 – user4150758