获取Photos.app中的图像数量?

时间:2012-09-29 01:28:49

标签: ios numbers photos alassetslibrary

我知道可以使用ALAssetsLibrary获取Photos.app中的图像,但如何获取Photos.app中的照片总数?

我正在尝试查看照片数量,因为我在Photos.app中获取了最后一张图片,其中包含此问题的代码:Get last image from Photos.app?

因此,如果设备上有0张图像,它将不会执行上述链接中的代码。

无论如何,我怎么能得到这个?

谢谢!

2 个答案:

答案 0 :(得分:6)

使用iOS 8中引入的新Photos框架,您可以使用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;
}];

这不包括智能相册(根据我的经验,他们没有有效的“估计数”),因此您可以选择获取资产以获得实际数量:

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;
}];

顺便说一下,如果你还没有请求授权库,你应该这样做,例如:

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

使用旧的AssetsLibrary框架,您可以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

答案 1 :(得分:0)

使用enumerateAssetsUsingBlock。每次结果都不是nil,将其添加到数组中(我将其称为self.arrayOfAssets)。然后,当你得到一个零结果(枚举的终点)得到self.arrayOfAssets.count

编辑:好的,所以这是来自其他问题的代码,只有几处更改。使用enumerateAssetsUsingBlock: instead of enumerateAssetsAtIndexes:

给自己一个可变数组并将每个图像放在那里。

然后,当资产为nil时,表示枚举已完成,请计算数组。

    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");
  }];