ios 获取所有相册的列表和每个相册的缩略图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9705478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:18:22  来源:igfitidea点击:

Get list of all photo albums and thumbnails for each album

iphoneiosios5

提问by nick

How do I get a list of all the photo albums in iOS? I know the image picker has to have a list of them somewhere since it displays them all to the user. Once I have the photo albums, how would I go about displaying thumbnails of all the images in a particular album? I can't seem to find anything in the docs, but I'm sure there's got to be a way to do it.

如何获取 iOS 中所有相册的列表?我知道图像选择器必须在某处列出它们,因为它会将它们全部显示给用户。一旦我有了相册,我将如何显示特定相册中所有图像的缩略图?我似乎在文档中找不到任何内容,但我确信一定有办法做到这一点。

回答by Filip Radelic

You can't get that kind of information from UIImagePickerControllerif that is what you are asking.

UIImagePickerController如果这就是您要问的,则您无法从中获得此类信息。

Take a look at AssetsLibraryframework. There is even sample codethat implements a custom image picker.

看看AssetsLibrary框架。甚至还有实现自定义图像选择器的示例代码

回答by lbsweek

Enumerate and get latest image and its thumbnail.

枚举并获取最新的图像及其缩略图。

ALAssetsLibrary *library = [[ALAssetsLibrary 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]];

    // Chooses the photo at the last index
    [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(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]];


            UIImage *latestPhotoThumbnail =  [UIImage imageWithCGImage:[alAsset thumbnail]];


            // Stop the enumerations
            *stop = YES; *innerStop = YES;

            // Do something interesting with the AV asset.
            //[self sendTweet:latestPhoto];
        }
    }];
} failureBlock: ^(NSError *error) {
    // Typically you should handle an error more gracefully than this.
    NSLog(@"No groups");
}];

回答by Stephen H King

The Assets Library framework is deprecated as of iOS 9.0. Instead, use the Photos framework, which in iOS 8.0 and later provides more features and better performance for working with a user's photo library.

自 iOS 9.0 起,资产库框架已弃用。相反,请使用照片框架,该框架在 iOS 8.0 及更高版本中为处理用户的照片库提供了更多功能和更好的性能。

Check Photosframework, and inspect: PHAsset, PHAssetCollection, and PHCollectionList, depending on your needs.

检查照片框架,并根据您的需要检查:PHAsset、PHAssetCollection 和 PHCollectionList。