-(void)selectManyPhotos
{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
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.groupArray addObject:group];
// Reload albums
[self performSelectorOnMainThread:@selector(readPhotos) withObject:nil waitUntilDone:YES];
};
// Group Enumerator Failure Block
void (^assetGroupEnumberatorFailure)(NSError *) = ^(NSError *error) {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"Album Error: %@ - %@", [error localizedDescription], [error localizedRecoverySuggestion]] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
NSLog(@"A problem occured %@", [error description]);
};
// Enumerate Albums
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:assetGroupEnumberatorFailure];
[pool release];
});
}
-(void)readPhotos
{
static int i = 0;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"enumerating photos");
[[self.groupArray objectAtIndex:i] enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if(result == nil)
{
return;
}
//ALAsset *asset = result
UIImage *image = [UIImage imageWithCGImage:[result thumbnail]];
[image retain];
NSString *fileName = [NSString stringWithFormat:@"ss%d.png",i];
NSString* path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:fileName];
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
i++;
[self.imageArray addObject:image];
[imageTableView reloadData];
}];
NSLog(@"done enumerating photos");
[pool release];
}