【问题标题】:How to add images to UITableView from phone gallery如何从手机图库中将图像添加到 UITableView
【发布时间】:2016-08-04 07:53:20
【问题描述】:
我在故事板UITableView 中创建了原型单元、TableViewController 和UITableViewCell。现在我想从手机图库中选择所有图片并将一张或多张保存到数据库中。但是此时我需要实现向tableview添加图片。我是iOS的初学者。谁能告诉我怎么做?给个链接或者代码?感谢您的提前
【问题讨论】:
标签:
ios
objective-c
uitableview
uiimage
【解决方案1】:
您可以使用AssetsLibrary framework 中的ALAssetsLibrary 从手机图库中获取所有图像。将每个图像保存到一个数组中。
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != nil) {
if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
NSURL *url= (NSURL*) [[result defaultRepresentation] url];
[library assetForURL:url
resultBlock:^(ALAsset *asset) {
UIImage *img = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]];
[imgArray addbject:img];
}
failureBlock:^(NSError *error){ NSLog(@"operation failed"); } ];
}
}
};
然后您可以使用此数组保存到 DB 或在 tableview 中显示。
对于在 TableView 中使用,您需要在原型单元格中添加 UIImageView,然后将数组中的图像相应地设置到单元格中。您的表格视图委托方法如下所示。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [imgArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CellId = @"ImageCell";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellId];
if (cell == nil) {
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellId];
}
UIImage *img = [imgArray objectAtIndex:indexPath.row];
cell.imgView.image = img;
return cell;
}
【解决方案2】:
如果你想从图库中选择图片并显示到 tableview,你可以在 tableView 或 CustomCell imageView 中使用 Custom ImageView
首先,您需要从图库中选择图像并将其保存到数组中
在此之前在 viewDidLoad 方法中分配和初始化数组
ViewController.m
#import "ViewController.h"
@interface ViewController ()
{
NSMutableArray *arrayImage;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
arrayImage = [[NSMutableArray alloc]init];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
imageView.image=image;
[arrayImage addObject:image];
picker.delegate =self;
[picker dismissViewControllerAnimated:YES completion:nil];
}
然后在tableView中
如果你自定义单元格
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"];
if(cell==nil)
{
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CustomTableViewCell" owner:self options:nil];
cell = nib[0];
}
cell.galleryImageView.image = [arrayImage objectAtIndex:indexPath.row];
return cell;
}
如果你使用默认的表格视图单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *strCell = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
}
cell.imageView.image = [arrayImage objectAtIndex:indexPath.row];
return cell;
}