Ios - A simple example for getting a list of photo albums?

I am trying to get a list of albums available on the device using the link from here :

So far, I have this in my view of DidLoad:

// 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); 

However, nothing is added to the group array. I expect to see 2 items from NSLog.

+6
source share
2 answers

It looks like the answer comes in the asyncGroupBlock answer list, but your NSLog appears right after the call. Thus, the groups will still be empty and until they are filled in the current thread.

How about adding a log to 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]; }; 
+4
source

For IOS9, the ALAsset library is deprecated. Instead, a new asset type called PHAsset was introduced in Framework Photos. You can get albums using the PHAssetCollection class.

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

PHAssetCollectionType defines the type of album. You can repeat the fetchResults results for each album.

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

The photo frame album is represented by PHAssetCollection.

+3
source

All Articles