0

Okay, so I let the user add an image in a uiimageView. When I try showing that same image on the previous view controller, (in my case the previous view controller is an image on a cell,) it gives me an error (though it does not say what kind of error it is).

Below is the code from the second view controller in which the user can choose a image (and it displays on that view controller without any problems).

-(void) handleImageGallery
{
    self.imagePicker = [[UIImagePickerController alloc] init];
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    self.imagePicker.delegate = self;
    [self presentViewController:self.imagePicker animated:YES completion:nil];
}

-(void) imagePickerController:(UIImagePickerController *)picker     didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSData *dataImage = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"], 1);
    UIImage *img = [[UIImage alloc] initWithData:dataImage];
    [self.imageView setImage:img];
    [self.imagePicker dismissViewControllerAnimated:YES completion:nil];
}

Here is the Code on the first ViewController:

- (IBAction)unwindToTableViewController:(UIStoryboardSegue *)sender
{
secondViewController *addVC = (AddViewController *)sender.sourceViewController;
UIImage *showImage = addVC.imageView.image;

    //myImage is an already defined NSMutablearray
    [myImage insertObject:textImage atIndex:0];

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

    [self.myTableView beginUpdates];
    [self.myTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.myTableView endUpdates]

}

The code gave me a error, so I added a image to xcode and replaced UIImage *showImage = addVC.imageView.image; with this: NSString *textImage = @"coffee.jpg"; and it gave me that image. But what I want is the image that the user chose in the second view controller to show in the firs one.

1 Answers1

0

So what you basically doing is passing data back from one view controller to another. There is a very good link explaining all this. Passing Data between View Controllers Hope this helps

Community
  • 1
  • 1
Krishna
  • 770
  • 2
  • 8
  • 21
  • I can pass data (both ways, forward and backwards,) but I just can't do it with an image. –  Nov 26 '14 at 07:39
  • Did you see the link? It is clearly defined that when passing data backward you need to use protocols. Check for an integer if it is getting passed backward in your case – Krishna Nov 26 '14 at 07:53
  • Yes I looked at the link, and I didn't see what I'm looking for. Can you maybe give me an example? –  Nov 26 '14 at 07:56
  • Look what you need to do is pass data backward before dismissing your secondviewcontroller. For doing that you will have to properly read and implement what is given in the link. While passing data backward from one controller to other, you need to implement protocols and delegates. So read the part "Passing data backward" carefully in the link. – Krishna Nov 26 '14 at 08:00
  • Can you please give an example? I'm not so advanced in objectiv c –  Nov 26 '14 at 08:04
  • It is an example indeed, You just need to follow the steps to pass data. – Krishna Nov 26 '14 at 08:26
  • It worked with textFields, so Ill post that up: NSString *textName = addVC.textFieldName.text; [name insertObject:textName atIndex:0]; –  Nov 26 '14 at 18:07