0

I have an image in main view controller and i want this image to be shown in new view controller...I know how to get string values between two scenes but no idea about images in storyboards....please help me out...

Is it right way to pass UIImage to another controller...

if UIImage *img1=[UIImage imageNamed:@"1.jpg"]; and UIImageView is *newimgview  for newViewController

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"imageview"])
    {
        newViewController *nvc=segue.destinationViewController;
        nvc.newimgview.image=img1;

    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Srinivasa Reddy
  • 77
  • 1
  • 2
  • 10

4 Answers4

1

just pass the UIImage pointer the same way you are doing for other data like NSString in performseguewithidentifier

Saurabh Passolia
  • 8,099
  • 1
  • 26
  • 41
1

If you have the image, you can just call it in your ViewController, no need to pass it.

[UIImage imageWithContentsOfFile:filePath];

or

[UIImage imageNamed:filename]

If you don't initially have the image you can pass it's pointer along like you would a string. Read up here for more info: Passing Data between View Controllers

Community
  • 1
  • 1
AdamT
  • 6,405
  • 10
  • 49
  • 75
0

You can pass the file path to the destination view controller or you could really pass the image if you want so. Add this method and try passing the image

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"yourImageTransferSegue"]) {

            YourImageTransferViewController *destViewController = segue.destinationViewController;
            destViewController.imageObject = self.imageToBeTransfered;

    }
}

In YourImageTransferViewController's ViewDidLoad or wherever you want use the image object as self.imageObject

Satheesh
  • 10,998
  • 6
  • 50
  • 93
0

Yes ...finally I got it...using this code

In case  UIImage *imag1=[UIImage imageNamed:@"some.png"];

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"imageview"])
    {
        newViewController *nvc=segue.destinationViewController;
        nvc.imag2=imag1; //imag2 is UIImage that globally declared in newViewController
    }
}

And in newViewController under ViewDidLoad or wherever u want to use...

self.imageview2.image=imag2;

Thanks for all ...

Srinivasa Reddy
  • 77
  • 1
  • 2
  • 10