سؤال

I'm reading lots of posts about this but I can't solve my problem... I'm trying to send an Image from one ViewController with Picker...to another ViewController but the image doesn't appear...

I have 2 VC:

HomeViewController.h:

#import <UIKit/UIKit.h>
#import "PhotoViewController.h"

@interface QuizTypeViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

- (IBAction)photo:(id)sender;

@end

HomeViewController.m (I'm getting the image correctly, i'm going to post just the segue code)

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];

PhotoViewController *controller = [PhotoViewController new];
controller.imageView.image = chosenImage;

[picker dismissViewControllerAnimated:YES completion:NULL];
[self performSegueWithIdentifier:@"viewPhoto" sender:self];

}

PhotoViewController.h

#import <UIKit/UIKit.h>

@interface PhotoViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@end

PhotoViewController.m - Nothing...

What I'm doing wrong? I don't know...

هل كانت مفيدة؟

المحلول

You should NOT new a PhotoViewController. When you call

[self performSegueWithIdentifier:@"viewPhoto" sender:self];

A PhotoViewController instance will be automatically created for you. What you should do is pass the chosen image to it. And in your PhotoViewController's some method (for example:viewDidLoad) to display it.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    PhotoViewController *photoViewController = segue.destinationViewController;
    photoViewController.image = self.chosenImage;
}

نصائح أخرى

You create new PhotoViewController in imagePickerController:didFinishPickingMediaWithInfo: but you don't push/present it in view hierarchy so it will be dismissed. The best way is pass image as a parameter in performSegueWithIdentifier:sender method:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
_tmp = chosenImage;

[picker dismissViewControllerAnimated:YES completion:NULL];
[self performSegueWithIdentifier:@"viewPhoto" sender: chosenImage];

}

and in prepareForSegue:segue: method get image from sender and pass it to destination view controller:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // TODO: check segue identifier
    PhotoViewController *vc = (PhotoViewController*)segue.destinationViewController;
    // Get the image 
    UIImage *img = (UIImage*)sender
    // Pass image to the new view controller.
    vc.imageView.image = img;
    //It can failed because your image view can not be created
    // You should use @property for UIImage, pass img to image and in view did load
    //assign imageView.image = image
}

The [self performSegueWithIdentifier:@"viewPhoto" sender:self]; will create a new instance of the ViewController by itself.

If you want it to show your instance you need to show with [self presentViewController :viewController animated:YES] or similar

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{

  UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
 _tmp = chosenImage;

  PhotoViewController *controller    = [self.storyboard instantiateViewControllerWithIdentifier:@"viewPhoto"];
controller.imageView.image = chosenImage;

[picker dismissViewControllerAnimated:YES completion:NULL];
[self presentViewController:controlle animated:YES completion:nil];
}

HomeViewController.m

PhotoViewController *controller = [PhotoViewController new];
controller.image = chosenImage;

PhotoViewController.h

@property (weak, nonatomic) IBOutlet UIImage *image;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;

PhotoViewController.m

imageView.image = image;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top