获取每张专辑的所有相册和缩略图列表

时间:2012-03-14 15:59:55

标签: iphone ios ios5

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

3 个答案:

答案 0 :(得分:20)

如果您要问的是UIImagePickerController,则无法从{{1}}获取此类信息。

看看AssetsLibrary框架。甚至sample code实现了自定义图像选择器。

答案 1 :(得分:19)

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

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

答案 2 :(得分:0)

从iOS 9.0开始,不推荐使用Assets Library框架。相反,使用Photos框架,在iOS 8.0及更高版本中,它可以为用户的照片库提供更多功能和更好的性能。

检查Photos框架,并检查:PHAsset,PHAssetCollection和PHCollectionList,具体取决于您的需求。