superGokuN
superGokuN

Reputation: 1424

get photo from iphone photo library and show in table view

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

Answers (3)

reason
reason

Reputation: 1

 NSMutableArray *assets = [[NSMutableArray alloc]init];

In above code add __block as follows:

__block NSMutableArray *assets = [[NSMutableArray alloc]init];

Upvotes: 0

superGokuN
superGokuN

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

iDhaval
iDhaval

Reputation: 7844

You can refer following link to solve your Issues

http://agilewarrior.wordpress.com/tag/alassetslibrary/

Upvotes: 3

Related Questions