iOS 8照片框架:获取iOS8所有相册的列表 [英] iOS 8 Photos framework: Get a list of all albums with iOS8

查看:337
本文介绍了iOS 8照片框架:获取iOS8所有相册的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在iOS8中获取所有集合的列表,包括相机胶卷(现在称为瞬间)?

How do I get a list of all collections, including the camera roll (which is now called moments), in iOS8?

在iOS 7中,我使用ALAssetGroup枚举块,但这不包括iOS时刻,这似乎相当于iOS7中的Camera Roll。

In iOS 7, I use ALAssetGroup enumeration block, but that doesn't include iOS moments which is seems to be equivalent to Camera Roll in iOS7.

    void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
    {
        if (group == nil) {// We're done enumerating
            return;
        }

        [group setAssetsFilter:[ALAssetsFilter allAssets]];
        if ([[sGroupPropertyName lowercaseString] isEqualToString:@"camera roll"] && nType == ALAssetsGroupSavedPhotos) {
            [_assetGroups insertObject:group atIndex:0];
        } else {
            [_assetGroups addObject:group];
        }
    };

    // Group Enumerator Failure Block
    void (^assetGroupEnumberatorFailure)(NSError *) = ^(NSError *error) {
        SMELog(@"Enumeration occured %@", [error description]);
    };

    // Enumerate Albums
    [_library enumerateGroupsWithTypes:kSupportedALAlbumsMask
                            usingBlock:assetGroupEnumerator
                          failureBlock:assetGroupEnumberatorFailure];
    }];


推荐答案

使用Photos Framework有点不同,你可以实现同样的结果,你只需要部分完成。

Using Photos Framework is a bit different, you can achieve the same result though, you just have to do it in parts.

1)获取所有照片(iOS8中的时刻,或之前的相机胶卷)

1) Get all photos (Moments in iOS8, or Camera Roll before)

PHFetchResult *allPhotosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];

如果您希望按创建日期订购它们,您只需添加 PHFetchOptions 喜欢这样:

Optionally if you want them ordered as by creation date, you just add PHFetchOptions like so:

PHFetchOptions *allPhotosOptions = [PHFetchOptions new];
allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];

PHFetchResult *allPhotosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:allPhotosOptions];

现在,如果你想要,你可以从 PHFetchResult 对象:

Now if you want you can get assets from the PHFetchResult object:

[allPhotosResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
    NSLog(@"asset %@", asset);
}];

2)获取所有用户相册(例如,仅显示包含至少一张照片的相册的其他类别) )

2) Get all user albums (with additional sort for example to only show albums with at least one photo)

PHFetchOptions *userAlbumsOptions = [PHFetchOptions new];
userAlbumsOptions.predicate = [NSPredicate predicateWithFormat:@"estimatedAssetCount > 0"];

PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:userAlbumsOptions];

[userAlbums enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {
        NSLog(@"album title %@", collection.localizedTitle);
}];

对于从<返回的每个 PHAssetCollection code> PHFetchResult * userAlbums 您可以像这样获取 PHAssets (您甚至可以将结果限制为仅包含照片资产):

For each PHAssetCollection that is returned from PHFetchResult *userAlbums you can fetch PHAssets like so (you can even limit results to include only photo assets):

PHFetchOptions *onlyImagesOptions = [PHFetchOptions new];
onlyImagesOptions.predicate = [NSPredicate predicateWithFormat:@"mediaType = %i", PHAssetMediaTypeImage];
PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:collection options:onlyImagesOptions];

3)获取智能相册

PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
[smartAlbums enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {
        NSLog(@"album title %@", collection.localizedTitle);
}];

智能相册需要注意的一点是 collection.estimatedAssetCount 可以返回 NSNotFound 。标题表明这是估计的。如果你想确定你必须执行获取的资产数量并获得如下计数:

One thing to note with Smart Albums is that collection.estimatedAssetCount can return NSNotFound if estimatedAssetCount cannot be determined. As title suggest this is estimated. If you want to be sure of number of assets you have to perform fetch and get the count like:

PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];

资产数量= assetsFetchResult.count

Apple有一个样本项目可以满足您的需求:

Apple has a sample project that does what you want:

https://developer.apple.com/library/content/samplecode/UsingPhotosFramework/ExampleappusingPhotosframework.zip (你必须是注册开发者才能访问此内容)

https://developer.apple.com/library/content/samplecode/UsingPhotosFramework/ExampleappusingPhotosframework.zip (you have to be a registered developer to access this)

这篇关于iOS 8照片框架:获取iOS8所有相册的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆