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

res*_*ing 6 iphone objective-c ios ios7

我正在尝试使用此处的参考来获取设备中可用的相册列表:

到目前为止,我在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);
Run Code Online (Sandbox Code Playgroud)

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

Gih*_*han 5

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

PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
Run Code Online (Sandbox Code Playgroud)

PHAssetCollectionType 定义专辑的类型。您可以迭代 fetchResults 来获取每个专辑。

[userAlbums enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {}];
Run Code Online (Sandbox Code Playgroud)

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


ans*_*ble 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];
};
Run Code Online (Sandbox Code Playgroud)