ios - 获取相册列表的简单示例?

时间:2014-01-15 07:12:46

标签: ios iphone objective-c ios7

我正在尝试使用the reference from here获取设备中可用的相册列表:

到目前为止,我在viewDidLoad中有这个:

// Get albums
NSMutableArray *groups = [NSMutableArray new];
ALAssetsLibrary *library = [ALAssetsLibrary new];

ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) {
    if (group) {
        [groups addObject:group];
    }
};
NSUInteger groupTypes = ALAssetsGroupAlbum;
[library enumerateGroupsWithTypes:groupTypes usingBlock:listGroupBlock failureBlock:nil];

NSLog(@"%@", groups);

但是,groups数组中没有添加任何内容。我期待看到NSLog中的2个项目。

2 个答案:

答案 0 :(得分:4)

看起来响应来自异步响应listGroupBlock,但是你的NSLog在调用之后就出现了。因此,组仍然是空的,并且不会在当前线程中填充。

如何在listGroupBlock中添加日志记录?

ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) {
    if (group) {
        [groups addObject:group];
    }
    NSLog(@"%@", groups);

    // Do any processing you would do here on groups
    [self processGroups:groups];

    // Since this is a background process, you will need to update the UI too for example
    [self.tableView reloadData];
};

答案 1 :(得分:3)

对于IOS9以上,ALAsset库已被弃用。相反,照片框架已经引入了一种名为PHAsset的新资产类型。您可以使用PHAssetCollection类检索相册。

PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:nil];

PHAssetCollectionType定义相册的类型。你可以迭代fetchResults来获得每张专辑。

[userAlbums enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {}];

照片框架中的相册由PHAssetCollection代表。