简体   繁体   English

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

[英]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. 我可以通过assetsLibrary访问所有图像,但无法在表格中显示它们。 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 感谢帮助

You can refer following link to solve your Issues 您可以参考以下链接来解决您的问题

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

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; 
}
 NSMutableArray *assets = [[NSMutableArray alloc]init];

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM