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

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

我试图检查照片的数量,因为我正在从这个问题的代码获取Photos.app中的最后一个图像:从Photos.app获取最后一个图像?

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

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

谢谢!

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

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

 NSUInteger __block count = 0; // Get smart albums (eg "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 (eg 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 AssetsLibrary

 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 

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

编辑 :好吧,所以这里是来自其他问题的代码与几个变化。 使用enumerateAssetsUsingBlock: instead of enumerateAssetsAtIndexes:

给自己一个可变的数组,并把每个图像在那里。

然后,当资产没有信号表明枚举已经完成时,对数组进行计数。

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