Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios - Simple example to get list of photo albums?

I'm trying to get a list of photo albums available in the device using the reference from here:

So far I have this in my 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);

However, nothing is added to the groups array. I am expecting to see 2 items from the NSLog.

like image 679
resting Avatar asked Jan 15 '14 07:01

resting


2 Answers

It looks like the response comes in the async response listGroupBlock, but your NSLog comes right after the call. So groups will still be empty and won't be populated in the current thread yet.

What about adding the logging in 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];
};
like image 112
ansible Avatar answered Oct 17 '22 22:10

ansible


For IOS9 onwards ALAsset library has been deprecated. Instead the Photos Framework has been introduced with a new asset type called PHAsset. You can retrieve albums using 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 the albums. You can iterated the fetchResults to get each album.

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

Album in photos framework is represented by PHAssetCollection.

like image 5
Gihan Avatar answered Oct 17 '22 21:10

Gihan