AssetsLibrary Framework

The AssetsLibrary framework is used for accessing photos and videos managed by photos application of ios.

AssetsLibrary having the following classes:

  • ALAsset
  • ALAssetsRepresentation
  • ALAssetsFilter
  • ALAssetsGroup
  • ALAssetsLibrary

To use ALAssetsLibrary in your project you need to add AssetsLibrary.Framework and import in a viewcontroller in which you want to get photos and videos being managed by photos application.

Below are steps to use assets library,

1. Declare object of ALAssetsLibrary in your viewcontroller.h file as follow,
ALAssetsLibrary *library;

2. Import header file for AssetsLibrary as follow,
#import

3. Copy the below code in your viewcontroller’s viewDidLoad method.

//This block of code is used to enumerate a single asset.
void (^myAssetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != NULL)
{
NSLog(@”See Asset: %@”, result);
[YourLocalArray addObject:result];
}
};

//This block of code used to enumerate ALAssetsGroup.
void (^myAssetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
[group enumerateAssetsUsingBlock:myAssetEnumerator];
}
[tableview reloadData];
[activity stopAnimating];
[activity setHidden:YES];
};
YourLocalArray = [[NSMutableArray alloc] init];
library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:myAssetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLog(@"Failure");
}
];

4. Copy the below code in your viewcontroller.m file.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [YourLocalArray count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @”Cell”;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

ALAsset *asset = [YourLocalArray objectAtIndex:indexPath.row];
[cell.imageView setImage:[UIImage imageWithCGImage:[asset thumbnail]]];
[cell.textLabel setText:[NSString stringWithFormat:@"Asset %d", indexPath.row+1]];
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ALAsset *asset = [YourLocalArray objectAtIndex:indexPath.row];
DetailViewController *detailViewController = [[ DetailViewController alloc] initWithNibName:@”DetailViewController” bundle:nil];
if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto])
{
detailViewController.myImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
detailViewController.isImage = TRUE;
}
else if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo])
{
detailViewController.videoUrl = [[asset defaultRepresentation] url];
detailViewController.isImage = FALSE;
}
[self presentModalViewController: detailViewController animated:YES];
[detailViewController release];
}

5. Now you are able to list the assets in your viewcontroller class with the thumbnail image of the assets.And on selecting a particular asset you will be able to see the full screen image or video depending on the type of asset.

6. The code for DetailViewController is as under
DetailViewController.h
#import
#import

@interface WebsiteViewController : UIViewController
{
IBOutlet UIImageView *imageView;
UIImage *myImage;
NSURL *videoUrl;
BOOL isImage;
MPMoviePlayerViewController *moviePlayer1;
UIApplication *app;
BOOL isBackGround;
BOOL isForeGround;
}

@property (nonatomic, retain) UIImage *myImage;
@property (nonatomic, readwrite) BOOL isImage;
@property (nonatomic, retain) NSURL *videoUrl;

- (IBAction)onDoneButton:(id)sender;

@end

DetailViewController.m

#import “DetailViewController.h”

@implementation DetailViewController

@synthesize myImage;
@synthesize isImage;
@synthesize videoUrl;

- (void)viewDidLoad
{
if (self.isImage)
{
// To Display Full Screen Image
imageView.image = myImage;
}
else
{
// To Play Video
imageView.hidden = TRUE;
app = [UIApplication sharedApplication];

moviePlayer1 = [[MPMoviePlayerViewController alloc] initWithContentURL:videoUrl];
[self.view addSubview:moviePlayer1.view];
moviePlayer1.view.frame = CGRectMake(0, 0, 320, 480);

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification
object:app];

isBackGround = TRUE;
isForeGround = FALSE;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer1.moviePlayer];

if ([moviePlayer1 respondsToSelector:@selector(setFullscreen:animated:)]) {
// Use the new 3.2 style API
moviePlayer1.moviePlayer.shouldAutoplay = YES;
[moviePlayer1.moviePlayer setFullscreen:YES animated:YES];
} else {
// Use the old 2.0 style API
[moviePlayer1.moviePlayer play];
}
}

[super viewDidLoad];
}

- (IBAction)onDoneButton:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
}

- (void)applicationDidEnterBackground:(NSNotification *)notification
{
if (moviePlayer1)
{
[moviePlayer1.moviePlayer pause];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification
object:app];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:app];
isBackGround = FALSE;
isForeGround = TRUE;
}

- (void)applicationWillEnterForeground:(NSNotification *)notification
{
if (moviePlayer1)
{
[moviePlayer1.moviePlayer play];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification
object:app];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:app];
isForeGround = FALSE;
isBackGround = TRUE;
}

- (void)movieFinishedCallback:(NSNotification*)aNotification {
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
if (isBackGround)
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:app];
}
else if (isForeGround)
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:app];
}
[self dismissModalViewControllerAnimated:YES];
}

@end

Tags: , , , , , , , , , , ,

Comments are closed.