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

Ios 将图像从UIImagePickerController传递到另一个ViewController,ios,objective-c,cocoa-touch,uiimagepickercontroller,Ios,Objective C,Cocoa Touch,Uiimagepickercontroller,我读了很多关于这个的帖子,但我不能解决我的问题。。。 我正在尝试使用Picker…将图像从一个ViewController发送到另一个ViewController,但图像未显示 我有两个VC: HomeViewController.h: #import <UIKit/UIKit.h> #import "PhotoViewController.h" @interface QuizTypeViewController : UIViewController <UIImagePick

我读了很多关于这个的帖子,但我不能解决我的问题。。。 我正在尝试使用Picker…将图像从一个ViewController发送到另一个ViewController,但图像未显示

我有两个VC:

HomeViewController.h:

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

@interface QuizTypeViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

- (IBAction)photo:(id)sender;

@end
PhotoViewController.h

#import <UIKit/UIKit.h>

@interface PhotoViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@end
#导入
@接口PhotoViewController:UIViewController
@属性(强,非原子)IBUIImageView*imageView;
@结束
PhotoViewController.m-无


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

您不应该
新建
PhotoViewController。当你打电话的时候

[self-PerformsgueWithIdentifier:@“viewPhoto”发件人:self]

将自动为您创建一个
PhotoViewController
实例。您应该做的是将所选图像传递给它。在您的
PhotoViewController
中有一些方法(例如:
viewDidLoad
)来显示它

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

[self-performsguewithidentifier:@“viewPhoto”发送者:self]将自行创建ViewController的新实例


如果您想让它显示您的实例,您需要使用
[自我呈现viewController:viewController动画:是]
或类似工具来显示它,您可以在imagePickerController:didFinishPickingMediaWithInfo:中创建新的PhotoViewController,但您不会在视图层次结构中推送/呈现它,因此它将被取消。最好的方法是将图像作为参数传递给performsguewithidentifier:sender方法:

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

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

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

}
在prepareforsgue:segue:method中,从发送方获取图像并将其传递给目标视图控制器:

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

你合成了imageview吗?@Ramdy在Xcode 5中使用
LLVM
不需要合成等等。所以,我们不需要使用合成@rckoenes@Ramdy不,如果您使用
LLVM
,它现在是必需的。您不再需要在ViewDiLoad上的
.m
中添加
@synthesis
,我必须插入以下内容:“self.imageView.image=self.image;”
@property (weak, nonatomic) IBOutlet UIImage *image;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
imageView.image = image;