ios – 简单的例子来获取相册列表?
我试图从这里获得设备中可用的相册列表:
到目前为止,我在我的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);
但是,没有添加到组数组。 我期待从NSLog看到2项。
看起来响应来自asynchronous响应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]; };
对于IOS9起,ALAsset库已被弃用。 相反,照片框架已经引入了一种名为PHAsset的新资产types。 您可以使用PHAssetCollection类检索相册。
PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil]; PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
PHAssetCollectionType定义相册的types。 您可以迭代fetchResults来获取每个专辑。
[userAlbums enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {}];
照片框架中的相册由PHAssetCollection表示。