【问题标题】:Get number of images in Photos.app?获取 Photos.app 中的图像数量?
【发布时间】:2012-09-20 21:04:53
【问题描述】:

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

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

所以如果设备上有 0 张图片,它不会执行上面链接中的代码。

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

谢谢!

【问题讨论】:

    标签: ios numbers photos alassetslibrary


    【解决方案1】:

    借助 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
    

    【讨论】:

    • 感谢您的快速编辑。我使用PHFetchResult *collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil]; 让我的工作,它给了我与默认Photos 应用程序完全相同的结果。
    • @atulkhatri - 非常正确,您必须使用PHAssetCollectionTypeSmartAlbum 来获取智能相册(例如“相机胶卷”等)。但是,您还必须使用PHAssetCollectionTypeAlbum 来获取其余专辑,例如那些从 iTunes 同步并由应用程序创建的。请参阅修改后的答案。
    【解决方案2】:

    使用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-02-10
    • 2017-01-07
    • 2017-11-23
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多