kenzolek
kenzolek

Reputation: 344

How to add images to UITableView from phone gallery

I created in storyboard UITableView with prototype cell, TableViewController and UITableViewCell. Now I would like to choose all the pictures from phone gallery and save one or more to the database. But at this moment I need to implement adding pictures to tableview. I'm beginner in iOS. Can anyone tell me how can do that? give me a link or code? Thank you for advance

Upvotes: 0

Views: 691

Answers (2)

user3182143
user3182143

Reputation: 9589

If you want to pick the image from gallery and show it to tableview, you can use Custom ImageView in tableView or CustomCell imageView

First you need to pick the image from gallery and save it to array

Before that allocate and initialize the array in viewDidLoad method

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

Then in tableView

If you 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;
}

If you use default table view 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;
}

Upvotes: 1

jrodrigues
jrodrigues

Reputation: 113

You can use ALAssetsLibrary in AssetsLibrary framework to fetch all images from phone gallery. Save each images in to an Array.

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

Then you can use this array for saving to DB or to show in tableview. For Using in TableView, you need to add UIImageView in your prototype cell, then set the images from the array to the cell accordingly. Your table view delegate methods will be like below.

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

Upvotes: 2

Related Questions