Sim*_*iwi 1 numbers photos ios alassetslibrary
我知道可以使用ALAssetsLibrary获取Photos.app中的图像,但如何获取Photos.app中的照片总数?
我正在尝试检查照片的数量,因为我正在使用此问题的代码获取Photos.app中的最后一张图片:从Photos.app获取最后一张图片?
因此,如果设备上有0个图像,它将不会执行上面链接中的代码.
无论如何我怎么能得到这个?
谢谢!
使用PhotosiOS 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;
}];
Run Code Online (Sandbox Code Playgroud)
这将不包括智能相册(根据我的经验,他们没有有效的"估计计数"),因此您可以选择获取资产以获得实际数量:
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;
}];
Run Code Online (Sandbox Code Playgroud)
顺便说一句,如果你还没有请求授权库,你应该这样做,例如:
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
// insert your image counting logic here
}
}];
Run Code Online (Sandbox Code Playgroud)
使用旧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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2442 次 |
| 最近记录: |