RGriffiths RGriffiths - 1 month ago 6
iOS Question

Take a photo and add to UITableView

I have an app that has a takePhoto

UIButton
on a
UIViewcontroller
that enables the user to take a photo by using the
imagePickerController
. I then want the photo to be placed in a
UITableView
.

The photo part works and for testing purposes I have the image displayed in a
UIImage
photoImageView.image

I have added the
UITableView
delegate and then
UITable
is called photoTable and the cell is called photoCell. I have tried to figure out the code below for adding the image to the table without success. Any help greatly appreciated.

- (IBAction)takePhoto:(id)sender
{
[self startCameraControllerFromViewController: self usingDelegate: self];
}

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image;
image = (UIImage *)
[info valueForKey:UIImagePickerControllerOriginalImage];
photoImageView.image=image;
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
[picker dismissViewControllerAnimated:YES completion:nil];
}

-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}

- (BOOL) startCameraControllerFromViewController: (UIViewController*) controller usingDelegate: (id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>) delegate
{
if (([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera] == NO) || (delegate == nil) || (controller == nil)) return NO;
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
cameraUI.allowsEditing = NO;
cameraUI.delegate = delegate;
[controller presentViewController:cameraUI animated:YES completion:nil];
return YES;
}

- (UITableViewCell*)tableView:(UITableView*)photoTable cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString* CellIdentifier = @"photoCell";

UITableViewCell* cell = [photoTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell.imageView.image = [UIImage imageNamed:@"nameOfImage.jpg"];

return cell;
}

Answer

create the image property, and tableview outlet in .h file

@property(nonatomic, strong) UIIImage *photoImage;
@property(nonatomic, strong) IBOutlet UITableView *tableview;

in .m file in didFinishPickingMediaWithInfo: method

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
    UIImage *image;
    image = (UIImage *)
    [info valueForKey:UIImagePickerControllerOriginalImage];
    self.photoImage = image;
    [self.tableView reloadData]; //call reloadData method to load data with image
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    [picker dismissViewControllerAnimated:YES completion:nil];
    }

in cellForRowAtIndexPath: method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        UITableViewCell *cell = [self.movieTable dequeueReusableCellWithIdentifier:@"cell"];
        cell.imageView.image = self.photoImage; 

       or use array and add image to array in didFinishPickingMediaWithInfo: and use the image

       cell.imageView.image = [self.photoArray objectAtIndex:0]; 

}