n many a application, we need to fetch photos from Photos application of the iOS device. And for that developer normally use UIImagePickerView that has a default layout and using that we can access photo library of the device. But in case we want to create custom views within the application for photo albums, for that we can use Assets Library Framework. This is used for accessing the photos and videos present in Photos Application of the iOS device. Below is a simple implementation that represent how to fetch the Album details and use it in the application. Below is the implementation of the logic: |
— Create a class subclass of NSObject for storing the Data: |
@interface AlbumObject : NSObject { NSString* sTitle; UIImage* posterImage; int nImageCount; } @property (nonatomic, retain) NSString* sTitle; @property (nonatomic, retain) UIImage* posterImage; @property (nonatomic, assign) int nImageCount; @end Synthesize and release all the retained instances in dealloc in it's implementation. -- Next, in the view Controller , we will import AssetsLibrary Framework and use it as: #pragma mark - View lifecycle - (void)viewDidLoad { [superviewDidLoad]; albumData = [[NSMutableArray alloc] init]; void (^assetEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) { if(group != nil) { NSString* myLib = [group valueForProperty:ALAssetsGroupPropertyName]; if ( [myLib length] ) { AlbumObject* anObject = [[AlbumObject alloc] init]; if ( anObject != nil ) { anObject.sTitle = [group valueForProperty:ALAssetsGroupPropertyName]; anObject.posterImage = [UIImage imageWithCGImage:[group posterImage]]; [albumData addObject: anObject]; [group setAssetsFilter:[ALAssetsFilter allPhotos]]; anObject.nImageCount = group.numberOfAssets; [anObject release]; } } } else { //Photo Application data retrieved ..... and we can use the album data // Reload Table or use the Album data in filling grid view. } }; ALAssetsLibrary* assetsLibrary = [[[ALAssetsLibraryalloc] init]autorelease]; [assetsLibraryenumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:assetEnumerator failureBlock:nil]; } In this we have defined a block named assetEnumerator and we have provided it as a parameter to the method: - (void)enumerateGroupsWithTypes:(ALAssetsGroupType)types usingBlock:(ALAssetsLibraryGroupsEnumerationResultsBlock)enumerationBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock;
A list of group will be matched for the given type and enumeration block will be executed for that many blocks. Next, when enumeration is done, enumeration block will be called with group set to nil.
Also, user will be asked for permission while using the above code as we are accessing data from photos application. If not allowed , then failure block will be called.
In the above case, nil is provided for failure block that can be replaced with required block including “popup for no photos avail”, etc. Also, in case data is not currently available then failure block is called.
Note: Please add AssetsLibrary framework and import it in your controller as:
#import