1

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

几乎我正在尝试检查照片的数量,因为我正在使用以下问题的代码获取 Photos.app 中的最后一张图片:Get last image from Photos.app?

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

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

谢谢!

4

2 回答 2

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
于 2012-09-29T01:41:13.870 回答
0

使用enumerateAssetsUsingBlock. 每次结果不为零时,将其添加到数组中(我将其称为self.arrayOfAssets)。然后,当你得到一个 nil 结果(枚举的终点)时,得到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");
  }];
于 2012-09-29T01:41:26.473 回答