Get number of images in Photos.app?

2019-02-28 08:19发布

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!

2条回答
你好瞎i
2楼-- · 2019-02-28 09:00

With the new Photos framework, introduced in iOS 8, you can use estimatedAssetCount:

NSUInteger __block estimatedCount = 0;

PHFetchResult <PHAssetCollection *> *collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
[collections enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull collection, NSUInteger idx, BOOL * _Nonnull stop) {
    estimatedCount += collection.estimatedAssetCount;
}];

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:

NSUInteger __block count = 0;

// Get smart albums (e.g. "Camera Roll", "Recently Deleted", "Panoramas", "Screenshots", etc.

PHFetchResult <PHAssetCollection *> *collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
[collections enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull collection, NSUInteger idx, BOOL * _Nonnull stop) {
    PHFetchResult <PHAsset *> *assets = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
    count += assets.count;
}];

// Get the standard albums (e.g. those from iTunes, created by apps, etc.), too

collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
[collections enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull collection, NSUInteger idx, BOOL * _Nonnull stop) {
    PHFetchResult <PHAsset *> *assets = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
    count += assets.count;
}];

And, by the way, if you haven't already requested authorization for the library, you should do so, e.g.:

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
    if (status == PHAuthorizationStatusAuthorized) {
        // insert your image counting logic here
    }
}];

With the old AssetsLibrary framework, you can enumerateGroupsWithTypes:

NSUInteger __block count = 0;

[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    if (!group) {
        // asynchronous counting is done; examine `count` here
    } else {
        count += group.numberOfAssets;
    }
} failureBlock:^(NSError *err) {
    NSLog(@"err=%@", err);
}];

// but don't use `count` here, as the above runs asynchronously
查看更多
趁早两清
3楼-- · 2019-02-28 09:12

use enumerateAssetsUsingBlock. every time result is not nil, add it to an array (I'll call it self.arrayOfAssets). Then, when you get a nil result (the terminal point of the enumeration) get self.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.

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 

    self.allPhotos = [[NSMutableArray alloc] init];


    // Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos                                                                   usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

    // Within the group enumeration block, filter to enumerate just photos.
    [group setAssetsFilter:[ALAssetsFilter allPhotos]];

    [group enumerateAssetsUsingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {

    // The end of the enumeration is signaled by asset == nil.
    if (alAsset) {
         ALAssetRepresentation *representation = [alAsset defaultRepresentation];
         UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];
        [self.allPhotos addObject:latestPhoto];
    if (!asset){
            NSLog:(@"photos count:%d", self.allPhotos.count);
            }
         }
    }];
 }
 failureBlock: ^(NSError *error) {
    // Typically you should handle an error more gracefully than this.
    NSLog(@"No groups");
  }];
查看更多
登录 后发表回答