且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS 8照片框架:获取iOS8所有相册的列表

更新时间:2023-02-03 09:03:55

使用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 $ c获得资产$ c>对象:

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 $如果无法确定estimatedAssetCount,则c $ c>可以返回 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)