I know it's possible to get the images in the Photos.app by using ALAssetsLibrary but how do I get the total number of photos in Photos.app?
Pretty much I am trying to check the number of photos because I am getting the last image in the Photos.app with the code from this question: Get last image from Photos.app?
So if there's 0 images on the device, it won't execute the code from the link above.
Anyway how can I get this?
Thanks!
With the new
Photos
framework, introduced in iOS 8, you can useestimatedAssetCount
:That won't include the smart albums (and, in my experience, they don't have valid "estimated counts"), so you can alternatively fetch the assets to get the actual count:
And, by the way, if you haven't already requested authorization for the library, you should do so, e.g.:
With the old
AssetsLibrary
framework, you canenumerateGroupsWithTypes
:use
enumerateAssetsUsingBlock
. every time result is not nil, add it to an array (I'll call itself.arrayOfAssets
). Then, when you get a nil result (the terminal point of the enumeration) getself.arrayOfAssets.count
.EDIT: Okay, so here's the code from the other question with a couple changes. Use
enumerateAssetsUsingBlock: instead of enumerateAssetsAtIndexes:
Give yourself a mutable array and put each image in there.
Then, when asset is nil signaling that enumeration has finished, count the array.