Saturday, October 13, 2012

Accessing Images in iPhone


In iPhone you can use the Assets Library framework to access the pictures and videos managed by the Photo application in the device. Have a look at framework reference by apple here.
I recenlty access all images in my iPhone device's photo application. I would like to share what i actually tried through this blog.

I first accessed all the albums in Photo appllication of my device. Then i enumerated each ato get all the images in each album. Below code takes all the pics and add to an Array.
To load albums use the code below. 

dispatch_async(dispatch_get_main_queue(), ^
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Group enumerator Block
void (^assetGroupEnumerator)( ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
if (group == nil){
return;
}
[self.assetGroups addObject:group];
NSLog(@"the total albums are : %d",self.assetGroups.count);
NSLog(@"count in thtread: %d", [group numberOfAssets]);//Displays count of total photos
};
// Group Enumerator Failure Block
void (^assetGroupEnumberatorFailure)(NSError *) = ^(NSError *error) {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"Album Error: %@", [error description]] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
NSLog(@"A problem occured %@", [error description]);
};
// Enumerate Albums
NSLog(@"enumerate assets in thread");
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:assetGroupEnumberatorFailure];
[library release];
[pool release];
})
Where the assetGroups is nothing but ALAssetGroup to which all the albums is added through the above code.
And now loop through each album and pic the assets init.

for(ALAssetsGroup *group in self.assetGroups)
{
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if(result == nil)
{
return;
}
NSLog(@" the assset are : %@", result);
Asset *asset = [[[Asset alloc] initWithAsset:result] autorelease];
[self.elcAssets addObject:asset];
NSLog(@" the assset are : %@", self.elcAssets);
}];
}
NSLog(@"album count : %d", self.assetGroups.count);
NSLog(@"enumerating photos");
NSMutableArray *selectedAssetsImages = [[[NSMutableArray alloc] init] autorelease];
for(Asset *elcAsset in self.elcAssets)
{
[selectedAssetsImages addObject:[elcAsset asset]];
}
Asset here is an array selectedAssestsImages ia an array to which i add all the looped assets. So finally you get all the assets in this array.
Thats how i programmed to take all the images from the Photos in my device.  For even more brief solution have a look at this
Happy blogging :)

No comments:

Post a Comment