图像选择器并将选择的图像推送到新的视图控制器

时间:2013-07-12 16:00:47

标签: ios uiviewcontroller uiimageview uiimage uiimagepickercontroller

我正在尝试使用相机拍摄照片,然后将所选照片传递到另一个视图控制器。我使用segue将图像推到下一个,但它似乎没有得到图像。

pragma mark - UIImagePickerDelegate方法

//delegate methode will be called after picking photo either from camera or library
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self dismissViewControllerAnimated:YES completion:nil];

    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    [self.collectedImageView setImage:image];

    [self performSegueWithIdentifier:@"photoArea" sender:self];
}

pragma mark - 准备Segue

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    MESSelectPhotoAreaViewController *photoAreaVC = [segue destinationViewController];

    photoAreaVC.photoImageView.image = self.collectedImageView.image;
}

1 个答案:

答案 0 :(得分:2)

我猜“photoAreaVC.photoImageView”仍然为null,因为目标viewController还没有完全实例化。

您可以通过在“prepareForSegue:”方法中添加此行来确认:

if (photoAreaVC.photoImageView == NULL)
    NSLog( @"photoImageView doesn't yet exist" );

您需要在目标视图控制器中找到一个地方(例如“UIImage”属性,或许?)暂时将该图像存储到“viewDidAppear:”或“viewWillAppear:”被调用,在“photoImageView”属性指向有效对象的地方。