代码之家  ›  专栏  ›  技术社区  ›  SimplyKiwi

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

  •  1
  • SimplyKiwi  · 技术社区  · 13 年前

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

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

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

    不管怎样,我怎么才能得到这个?

    谢谢

    2 回复  |  直到 9 年前
        1
  •  7
  •   Rob    10 年前

    随着新的 Photos 框架,在iOS 8中引入,您可以使用 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
    
        2
  •  0
  •   Akshit Zaveri    12 年前

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

    编辑 :好的,这是另一个问题的代码,有几个改动。使用 enumerateAssetsUsingBlock: instead of enumerateAssetsAtIndexes:

    给自己一个可变数组,并将每个图像放入其中。

    然后,当asset为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");
      }];