将图像从UIImagePickerController传递到另一个ViewController

时间:2014-04-02 09:20:42

标签: ios objective-c cocoa-touch uiimagepickercontroller

我正在阅读很多有关此事的帖子,但我无法解决我的问题...... 我试图将一个带有Picker的ViewController发送到另一个ViewController,但是图像没有显示......

我有2个VC:

HomeViewController.h:

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

@interface QuizTypeViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

- (IBAction)photo:(id)sender;

@end

HomeViewController.m (我正确地获取图片,我将仅发布segue代码)

- (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 - 没什么......

我做错了什么?我不知道......

5 个答案:

答案 0 :(得分:1)

你不应该new一个PhotoViewController。当你打电话

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

将自动为您创建PhotoViewController个实例。你应该做的是将选择的图像传递给它。并在PhotoViewController的某种方法(例如:viewDidLoad)中显示它。

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

答案 1 :(得分:1)

您在imagePickerController中创建了新的PhotoViewController:didFinishPickingMediaWithInfo:但您不会在视图层次结构中推送/显示它,因此它将被解除。最好的方法是将图像作为参数传递给performSegueWithIdentifier:sender方法:

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

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

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

}

并在prepareForSegue中:segue:方法从发件人获取图像并将其传递给目标视图控制器:

- (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
}

答案 2 :(得分:0)

[self performSegueWithIdentifier:@"viewPhoto" sender:self];将自己创建一个ViewController的新实例。

如果您希望它显示您的实例,则需要使用[self presentViewController :viewController animated:YES]或类似的

进行展示

答案 3 :(得分:0)

 - (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];
}

答案 4 :(得分:0)

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;