xcode 从 iphone 照片库中获取照片并在表格视图中显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10174272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get photo from iphone photo library and show in table view
提问by superGokuN
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.
我想在表格视图中显示照片库中的所有图像。我可以通过 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");}];
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
感谢帮助
采纳答案by iDhaval
You can refer following link to solve your Issues
您可以参考以下链接来解决您的问题
回答by superGokuN
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;
}
回答by reason
NSMutableArray *assets = [[NSMutableArray alloc]init];
In above code add __block
as follows:
在上面的代码中添加__block
如下:
__block NSMutableArray *assets = [[NSMutableArray alloc]init];