Objective-C photos and videos programmatically

Objective-C access photos video: how to access programatically to photos and videos which are under the control of the Photos application.

Photos App
Photos App

Photo application contains saved photos (for example saved from Safari), imported from iTunes and imported directly  into the device. There are 3 elements we have to consider :

  1. library (ALAssetsLibrary)” as list of   groups” :
    • the main class is ALAssetsLibrary that provides facilities in order to navigate into Photos Library.
    Asset Libary
    Asset Libary
  2. groups (ALAssetsGroup)”  as list of    “assets” :
    • ALAssetsLibrary allows to discover groups (ALAssetsGroup) of resources such as “Saved Photos.
    Asset Groups
    Asset Groups
  3. assets (ALAsset)” are multimedia elements :
    • ALAssetsGroup allows to discover the list of final resources (ALAsset).
Objective-C access photos video
Objective-C access photos video

So in order to get all resources you have to list (enumerate) ALAssetsGroup from ALAsstetsLibrary and for each group you have to list (enumerate) ALAssets.
Below an example ready to use :

  • use of ALAssetsLibrary / ALAssetsGroup / ALAssets / ALAssetRepresentation
  • display the image form asset


#1 Add framework : AssetsLibrary.framework


#2 Include in your code

#import <AssetsLibrary/AssetsLibrary.h> 

#3 Explore Library

Use the following example code (I used the start point of App)
Note: ALAssetsLibrary / ALAssetsGroup are asynchronously so you are not blocked on enumeration.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    UIImageView *iv = [[UIImageView alloc] initWithFrame:self.window.frame];
    [self.window addSubview:iv];
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    // ---> ALAssetsLibrary
    // http://developer.apple.com/library/ios/#documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html#//apple_ref/occ/cl/ALAssetsLibrary
    [library enumerateGroupsWithTypes:ALAssetsGroupAll
             usingBlock: ^(ALAssetsGroup *group, BOOL *stop){
                               // ---> ALAssetsGroup :
                               // http://developer.apple.com/library/ios/#documentation/AssetsLibrary/Reference/ALAssetsGroup_Class/Reference/Reference.html#//apple_ref/occ/instm/ALAssetsGroup/enumerateAssetsUsingBlock:
                               NSLog(@"Name is '%@'",[group valueForProperty:ALAssetsGroupPropertyName]);
                               NSLog(@"Type is '%@'",[group valueForProperty:ALAssetsGroupPropertyType]);
                               NSLog(@"PTID is '%@'",[group valueForProperty:ALAssetsGroupPropertyPersistentID]);
                               NSLog(@"URL    is '%@'",[group valueForProperty:ALAssetsGroupPropertyURL]);
                               [group enumerateAssetsUsingBlock:  ^(ALAsset *result, NSUInteger index, BOOL *stop){
                                       // ---> ALAsset:
                                       // http://developer.apple.com/library/ios/#documentation/AssetsLibrary/Reference/ALAsset_Class/Reference/Reference.html#//apple_ref/doc/c_ref/ALAsset
                                       NSLog(@"     Stop? %@", (stop ? @"YES" : @"NO") );
                                       NSLog(@"     Type is '%@'",[result valueForProperty:ALAssetPropertyType]);
                                       NSLog(@"     Loca is '%@'",[result valueForProperty:ALAssetPropertyLocation]);
                                       NSLog(@"     Dura is '%@'",[result valueForProperty:ALAssetPropertyDuration]);
                                       NSLog(@"     Orie is '%@'",[result valueForProperty:ALAssetPropertyOrientation]);
                                       NSLog(@"     Date is '%@'",[result valueForProperty:ALAssetPropertyDate]);
                                       NSLog(@"     Rapr is '%@'",[[result valueForProperty:ALAssetPropertyRepresentations] objectAtIndex:0]);
                                       NSLog(@"     URLs is '%@'",[[result valueForProperty:ALAssetPropertyURLs] objectForKey:[[result valueForProperty:ALAssetPropertyRepresentations] objectAtIndex:0]]);
                                   if ([@"public.jpeg" caseInsensitiveCompare:[[result valueForProperty:ALAssetPropertyRepresentations] objectAtIndex:0]] == NSOrderedSame ) {
                                       NSLog(@"Image %@",[[result valueForProperty:ALAssetPropertyURLs] objectForKey:@"public.jpeg"]);
                                       *stop = YES; // remove in order to list all resources
                                       ALAssetRepresentation *arep = [result defaultRepresentation];
                                       UIImage *img = [UIImage imageWithCGImage:[arep fullResolutionImage]];
                                       [iv setImage:img];
                           failureBlock: ^(NSError *error) {
    return YES;

Output :

[24212:17903] Application windows are expected to have a root view controller at the end of application launch
[24212:17903] -----------------
[24212:17903] Name is 'Saved Photos'
[24212:17903] Type is '16'
[24212:17903] PTID is '20884EF8-47A0-46DE-BFDD-045844E417B9'
[24212:17903] URL  is 'assets-library://group/?id=20884EF8-47A0-46DE-BFDD-045844E417B9'
[24212:17903]   Stop? YES
[24212:17903]   Type is 'ALAssetTypePhoto'
[24212:17903]   Loca is '(null)'
[24212:17903]   Dura is 'ALErrorInvalidProperty'
[24212:17903]   Orie is '0'
[24212:17903]   Date is '2012-05-06 16:39:11 +0000'
[24212:17903]   Rapr is 'public.jpeg'
[24212:17903]   URLs is 'assets-library://asset/asset.JPG?id=97CBE757-72D9-475A-8436-216A9FF87E04&ext=JPG'
[24212:17903] Image assets-library://asset/asset.JPG?id=97CBE757-72D9-475A-8436-216A9FF87E04&ext=JPG
[24212:17903]   Stop? YES
[24212:17903]   Type is '(null)'
[24212:17903]   Loca is '(null)'
[24212:17903]   Dura is '(null)'
[24212:17903]   Orie is '(null)'
[24212:17903]   Date is '(null)'
[24212:17903]   Rapr is '(null)'
[24212:17903]   URLs is '(null)'
[24212:17903] -----------------
[24212:17903] Name is '(null)'
[24212:17903] Type is '(null)'
[24212:17903] PTID is '(null)'
[24212:17903] URL  is '(null)'
User Review
0 (0 votes)