在segue之后,无法在下一个ViewController上从UIImagePickerController加载图像

时间:2013-03-09 12:40:01

标签: ios objective-c uiimagepickercontroller uistoryboardsegue

我正在尝试在下一个视图控制器上加载UIImagePickerController上选择的图片。我对类似的线程进行了一些搜索,发现了一些帮助我设置的线程,但图像并没有真正“转移”到下一个视图控制器。

这是我的didFinishPickingImage和prepareForSegue的代码:

- (void)imagePickerController:(UIImagePickerController *)picker
                              didFinishPickingImage:(UIImage *)image 
                              editingInfo:(NSDictionary *)editingInfo
{
    self.imageChosen = image;
    [picker dismissModalViewControllerAnimated:NO];
    [self performSegueWithIdentifier:@"Edit" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
    if ([segue.identifier isEqualToString:@"Edit"]){
    EditViewController *editViewController = 
                       (EditViewController *)segue.destinationViewController;
    editViewController.imageView.image = self.imageChosen;
}

我让segue运行到EditViewController,但是imageView没有加载图片。我猜这个任务在某种程度上是错误的,但我没有看到如何。

1 个答案:

答案 0 :(得分:1)

当您尝试将图片设置为EditViewController UIImageView时,它不存在,因为EditViewController尚未加载。而不是将图像设置为UIImageView直接在EditViewController中创建将保存图像的实例变量。而不是将此图片分配到UIImageView

中的viewDidLoad:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
    if ([segue.identifier isEqualToString:@"Edit"]){
    EditViewController *editViewController = 
                   (EditViewController *)segue.destinationViewController;
    editViewController.image = self.imageChosen;
}

//EditViewController.h
...
@property (nonatomic, strong) UIImage *image;
...

//EditViewController.m
- (void)viewDidLoad {
    ...
    self.imageView.image = self.image;
    ...
}