Programmatically get list of all photo albums & image

Introduction to programmatically Get list of all photo albums and their images in IOS :

We can get album names from the photo gallery of iPhone programmatically by using Asset Library Framework.

In this post, we will showcase you about getting the album name programmatically from iPhone gallery and compare it with our local in app Gallery name to get all images from that album. So let us dive into the tutorial and get album names created by the user in his photo gallery of the iPhone.

Jump to code to Programmatically Get list of all photo albums and their images in IOS

Create a new project and name it AlbumName. Now add AssetLibrary framework to your project by selecting a project and click plus icon

add AssetLibrary Frmaework

Search for AssetLibrary Framework and select it, click add

Add assetlibrary framework from dropdown

Open ViewController.h file and import the AssetLibrary framework

  
#import AssetsLibrary/AssetsLibrary.h
 
Now create an instance of ALAssetsLibrary.
 
ALAssetsLibrary *library;
 

Complete Code:
#import UIKit/UIKit.h
#import AssetsLibrary/AssetsLibrary.h

@interface ViewController : UIViewController {
    ALAssetsLibrary *library;
}
@end
Open ViewController.m file and in its ViewDidLoad method, we will write our code for getting album name. Since album names were returned inside a block so if we want to compare our album name whether it exists or not. For this we have to create a local string variable albumNameString with the __block keyword in front so that it can be accessed inside the block and alloc the library instance so that we can get photos app permission from user.

  
__block NSString *albumNameString = @”Words Of Wisdom”;
   library = [[ALAssetsLibrary allocinit];
     
Now, it’s time to loop through all album names created by the user inside the gallery of their iPhone. For this, we will use the enumerateGroupsWithTypes method of ALAssetsLibrary.

[
library enumerateGroupsWithTypes:ALAssetsGroupAlbum
                           usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
         NSLog(@”album name == %@”,[group valueForProperty:ALAssetsGroupPropertyName]);
     } failureBlock: ^(NSError *error){
         NSLog(@”Failed to get list of albums”);
     }];
In the above code, we will loop through all albums created by the user in the iPhone photos app and print those names in NSLog.
Now, it’s time to compare our album name, whether it’s already exist or not. If it exists, then get all images of that album. Below is the code to compare album name and get images of that particular album


i
f ([albumNameString compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame)
        {
            [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
             {
                 NSLog(@”index == %d”,index);
                 NSLog(@”image %@”,[UIImage imageWithCGImage:[[result defaultRepresentationfullScreenImage]]);
             }];
        }

Above code compares the album name and if it exists on user, device, then we will loop through all images and print it in NSLog.

Complete Code:

– (void)viewDidLoad
{
    __block NSString *albumNameString = @”Words Of Wisdom”;
    library = [[ALAssetsLibrary allocinit];
    [library enumerateGroupsWithTypes:ALAssetsGroupAlbum
                        usingBlock:^(ALAssetsGroup *group, BOOL *stop)
    {
        NSLog(@”album name == %@”,[group valueForProperty:ALAssetsGroupPropertyName]);
        
        //compare the names of the albums
        if ([albumNameString compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame)
        {
            [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
             {
                NSLog(@”index == %d”,index);
                NSLog(@”image %@”,[UIImage imageWithCGImage:[[result defaultRepresentationfullScreenImage]]);
                                    }];
            }
    }
    failureBlock: ^(NSError *error)
    {
        NSLog(@”Failed to get list of albums”);
    }];
                                
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
 
 
Source Code: Download Code here

Where to go from here: 

In this post we learn How to Programmatically Get list of all photo albums and their images in IOS. If any questions then feel free to ask. Happy coding 🙂