Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get screenshot (album) count in swift

I am new to Swift and am trying to make a function that simply returns the number of photos in your screenshot album using fetchAssetCollections

I have

func getNumScreenshots() -> Int {

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

    return collection.count
}

However, this is always returning 3, and I'm not sure why (I have 600 screenshots on my iPhone).

like image 724
rjvalenti Avatar asked Mar 10 '23 21:03

rjvalenti


1 Answers

You can try with this:

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
        }
    })

Note: Use this code is Swift 3

like image 87
Sour LeangChhean Avatar answered Mar 28 '23 16:03

Sour LeangChhean