iosobjective-calassetslibrary

Objective-C / ALAssetsLibrary - Find image information and Exif information


I'm trying to use the ALAssetsLibrary Class to retrieve the list of the photos inside the device and the relative information about them. I have implemented a method that reveals correctly the number of images, but at the moment, I don't know how I can find photo information like width, height, orientation etc… This is the code that I'm using:

ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    if (group) {
        [group setAssetsFilter:[ALAssetsFilter allPhotos]];
        [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){
            if (asset){
                NSString *description = [asset description];
                NSLog(@"description %@", description);
                
            }
        }];
    }
} failureBlock:^(NSError *error) {
    NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];

Actually if I log the my NSString "description" variable, I receive this information (for one example image):

"description ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=766424BD-D28D-47F9-8E0F-AD8F19C4C732&ext=JPG"

Now, I need to understand how to find other pieces of information (width, height, name etc..) and if there is the possibility to access to the EXIF information of the images.


Solution

  • You were close. Instead of logging the description of the asset, you want to log the meta data of the default representation of the asset. The following will get you an NSDictionary containing the metadata for the image. Then you can access properties like height, width, etc with standard objectForKey calls.

    ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
    [library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        if (group) {
            [group setAssetsFilter:[ALAssetsFilter allPhotos]];
            [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){
                if (asset){
    
                    NSDictionary *data = [[asset defaultRepresentation] metadata];
                    NSLog(@"%@",data);
                }
            }];
        }
    } failureBlock:^(NSError *error) {
        NSLog(@"error enumerating AssetLibrary groups %@\n", error);
    }];