0

So I am building a IOS app and i have a photo gallery that shows up, when you press a photo it registers and you can do something with that photo. basically what I need is when a user presses that photo I need it to open up another screen (segue) and I need to know on that new screen what that particular photo that was pressed is and set a screen sized UIViewController as that photo. the code that i currently have is

      PhotoEditorViewController *editor = [[PhotoEditorViewController alloc] init];
ALAsset *asset = self.assets[indexPath.row];
ALAssetRepresentation *defaultRep = [asset defaultRepresentation];
editor.img = [UIImage imageWithCGImage:[defaultRep fullScreenImage] scale:[defaultRep scale] orientation:UIImageOrientationRight];
NSLog(@"Photo Pressed");
NSLog(@"%@", editor.img);

[self presentModalViewController:editor animated:YES];

so what this currently is doing is getting the photo that is pressd and setting my UIImage var. "img" as that image then when the segue is called i can see on the other screens viewDidLoad method that the image transferred over but for some reason i am unable to set that image as the UIImageViwer.

NSLog output:

On both screen one and screen two is shows UIImage 0x#######

Which makes me believe that the photo ID information is transferring however it’s not updating the uiimageview

    ViewDidLoad
        {
             NSLog(@“%hhu%”, editorPhoto);
             [image setImage:editorPhoto];
        }
  • Please show the PhotoEditorViewController code that is supposed to be "updating the UIImageView" on the "second screen". – matt Sep 03 '17 at 19:59
  • @matt I added the code not home so I only added the important code. So when the code runs it prints the right stuff but if you breakpoint as setImage it shows that the image is null – Fosterdn007 Sep 03 '17 at 20:04

2 Answers2

0

Use the 'prepare for Segue' method to pass data between ViewControllers:

create a UIImage in PhotoEditorViewController to hold the passed image

@IBOutlet weak var passedImage: UIImageView!

In your first ViewController

Swift 3.0

@IBOutlet weak var selectedImage: UIImageView!

override fun prepare(for segue: UIStorybordSegue, sender: Any?) {
      //set destination to PhotoEditorViewController
      if let destination = segue.destination as? 
      PhotoEditorViewController {

      //send image to 'passedImage' in PhotoEditorViewController
      destination.passedImage = selectedImage.image
}

Objectivce-C

see How to pass prepareForSegue: an object

flexr
  • 1
  • 2
0

Create the property in nextViewController.h file.

@property (strong, nonatomic) UIImageView *imgView;

Pass Image from here.

- (IBAction)nextView:(UIButton *)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    myViewController *VC = [storyboard instantiateViewControllerWithIdentifier:@"myViewControllerID"];
    VC.imgView.image = [UIImage imageNamed:@"image.jpg"];
    [self.navigationController pushViewController:VC animated:YES];
}
vipinsaini0
  • 541
  • 1
  • 7
  • 26