从iphone照片库中获取照片并在表格视图中显示

时间:2012-04-16 12:40:55

标签: iphone xcode alassetslibrary

我想在表格视图中显示照片库中的所有图像。我可以通过assetsLibrary访问所有图像,但无法在表格中显示它们。我没有收到任何错误,但仍然不知道发生了什么。

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");}];

表格视图的数据源方法:

- (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;

}

请求帮助我做错了......

感谢您的帮助

3 个答案:

答案 0 :(得分:3)

您可以参考以下链接来解决您的问题

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

答案 1 :(得分:1)

ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];

从库实例返回的对象的生命周期与库实例的生命周期相关联。我添加了一个静态方法来检索该类的共享实例。

+ (ALAssetsLibrary *)defaultAssetsLibrary {

    static dispatch_once_t pred = 0;

    static ALAssetsLibrary *library = nil;

    dispatch_once(&pred, ^{

    library = [[ALAssetsLibrary alloc] init];

    });

   return library; 
}

答案 2 :(得分:0)

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

在上面的代码中添加__block,如下所示:

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