2016-08-04 49 views
0

我在故事板UITableView中创建了原型单元格,TableViewController和UITableViewCell。现在我想选择手机图库中的所有图片并将一个或多个图片保存到数据库中。但在这一刻,我需要实现添加图片到tableview。我是iOS的初学者。谁能告诉我怎么能做到这一点?给我一个链接或代码?感谢您的提前如何将图像添加到手机图库中的UITableView

+0

试试这个http://stackoverflow.com/questions/12633843/get-all -of-the-pictures-from-an-iphone-photolibrary-in-an-an-an-array-using-assetslibr – sanjeet

+0

kenzolek检查我的回答 – user3182143

+0

我的回答对你有帮助吗? – user3182143

回答

2

您可以使用ALAssetsLibraryAssetsLibrary framework从手机库中获取所有图像。将每个图像保存到一个数组中。

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

然后,您可以使用此数组保存到数据库或在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; 
} 
1

如果您想选择从图库中的图像,并将其展示给tableview中,你可以使用自定义的ImageView中的tableView或CustomCell 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

如果CustomCell

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