本文介绍了获取Photos.app中的图像数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know it's possible to get the images in the Photos.app by using ALAssetsLibrary but how do I get the total number of photos in Photos.app?

漂亮很多我试图检查照片的数量,因为我在Photos.app中获得了最后一张图片,其中包含了这个问题的代码:

Pretty much I am trying to check the number of photos because I am getting the last image in the Photos.app with the code from this question: Get last image from Photos.app?

因此,如果设备上有0张图片,则无法执行来自上面链接的代码。

So if there's 0 images on the device, it won't execute the code from the link above.

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

Anyway how can I get this?

谢谢!

推荐答案

使用iOS 8中引入的新 Photos 框架,您可以使用 estimatedAssetCount

With the new Photos framework, introduced in iOS 8, you can use 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;
}];

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

That won't include the smart albums (and, in my experience, they don't have valid "estimated counts"), so you can alternatively fetch the assets to get the actual count:

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

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

And, by the way, if you haven't already requested authorization for the library, you should do so, e.g.:

[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

这篇关于获取Photos.app中的图像数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:23