Reputation: 1424
I want to show all the images from the photo library in a table view. I am able to access all the images via assetsLibrary but unable to show them in table. I am not getting any kind of error but still don't know what is going on.
NSMutableArray *assets = [[NSMutableArray alloc]init];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop){
if(group != NULL){
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop){
if(result != NULL){
[assets addObject:result];
}else NSLog(@"NO photo");;
}];
}
}
failureBlock:^(NSError *error){NSLog(@"Error");}];
Datasource method for table view:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *identifier = @"id";
UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if(cell == NULL){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
[cell.imageView setImage:[UIImage imageWithCGImage:[[assets objectAtIndex:indexPath.row]thumbnail]]];
[cell textLabel].text = @"Photo";
return cell;
}
Pleaes help what i am doing wrong....
thanks for help
Upvotes: 4
Views: 8244
Reputation: 1
NSMutableArray *assets = [[NSMutableArray alloc]init];
In above code add __block
as follows:
__block NSMutableArray *assets = [[NSMutableArray alloc]init];
Upvotes: 0
Reputation: 1424
ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
The lifetimes of objects you get back from a library instance are tied to the lifetime of the library instance.I added a static method to retrieve a shared instance of that class.
+ (ALAssetsLibrary *)defaultAssetsLibrary {
static dispatch_once_t pred = 0;
static ALAssetsLibrary *library = nil;
dispatch_once(&pred, ^{
library = [[ALAssetsLibrary alloc] init];
});
return library;
}
Upvotes: 1
Reputation: 7844
You can refer following link to solve your Issues
http://agilewarrior.wordpress.com/tag/alassetslibrary/
Upvotes: 3