在视图控制器之间传递图像

时间:2015-07-27 09:22:38

标签: ios objective-c image uiimageview uiimagepickercontroller

我读了很多关于这个问题的帖子,但我没有找到任何答案。 所以,我想从我的" ViewController"到我的" SecondViewController"从ImagePicker中挑选的图像,这是我尝试过的:

在我的ViewController中:

 -(IBAction)Scatta:(id)sender {

        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            [imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
        }

        // image picker needs a delegate
        [imagePickerController setDelegate:self];

        // Place image picker on the screen
        [self presentViewController:imagePickerController animated:YES completion:nil];   

        // Create main view controller
        Main_VCViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"Main"];
        // Go to main
        [self presentModalViewController:VC animated:YES];  
}

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

       [self dismissViewControllerAnimated:YES completion:^{

          UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
          Main_VCViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"Main"];
          VC.ImmaginePresa = image;
       [self presentModalViewController:VC animated:YES];

    }]; 
}

这是我的SecondViewController:

-(void)viewDidAppear:(BOOL)animated {
    UIImage *imm = _ImmaginePresa;
    [_Image setImage: imm];
}

我在SecondVC中设置了一个默认图像,当我启动应用程序时,按下" Scatta"按钮,当它打开我的SecondVC时,它向我显示我的默认图像0.5 / 1秒,之后,它没有显示任何内容。 我尝试以相同的方式传递一些字符串并且它可以正常工作。

EDIT1

的ViewController:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];


     //Instanzio il viewController della main
   Main_VCViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"Main"];

    NSData *imageData = UIImagePNGRepresentation(img.images);
    [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:imageData] forKey:@"ImageData"];

    VC.ImmaginePresa = img;

    [self dismissModalViewControllerAnimated:YES];
     //Vado alla main
   [self presentModalViewController:VC animated:YES];

}

SecondVC:

-(void)viewDidAppear:(BOOL)animated {
    NSData* myEncodedImageData =[[NSUserDefaults  standardUserDefaults]objectForKey:@"ImageData"];
    UIImage* image = [UIImage imageWithData:myEncodedImageData];
    [_Image setImage: image];
}

它仍然无效,结果相同

EDIT2

使用@SaurabhPrajapati的解决方案,似乎图像是零,因为当我点击我的图像时,它没有做任何事情可能吗?

1 个答案:

答案 0 :(得分:2)

我假设你正在" didFinishPickingMediaWithInfo"中获得图像。所以这是我的建议

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

UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
if(image != nil)
{
      [self dismissViewControllerAnimated:YES completion:^{

       //define ImmaginePresa as NSData in Main_VCViewController
       Main_VCViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"Main"];
       VC.ImmaginePresa = UIImagePNGRepresentation(image);
       [self presentModalViewController:VC animated:YES];
}]; 
}

}

在Main_VCViewController中你可以获得像

这样的图像
UIImage *img = [UIImage imageWithData:self.ImmaginePresa];