【问题标题】:get photo from iphone photo library and show in table view从 iphone 照片库中获取照片并在表格视图中显示
【发布时间】:2012-04-16 12:40:55
【问题描述】:

我想在表格视图中显示照片库中的所有图像。我可以通过 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;

}

请帮助我做错了什么......

感谢帮助

【问题讨论】:

    标签: iphone xcode alassetslibrary


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:
      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; 
      }
      

      【讨论】:

        【解决方案3】:
         NSMutableArray *assets = [[NSMutableArray alloc]init];
        

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-12-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-02-03
          • 1970-01-01
          • 2012-04-04
          相关资源
          最近更新 更多