0

我在 iOS 中使用下面的代码从照片库中选择图像。我需要在不同的视图中显示此图像。我应该如何进行?

//delegate methode will be called after picking photo either from camera or library
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self dismissViewControllerAnimated:NO completion:nil];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    NSArray *theViewControllers = [self.tabBarController viewControllers];

    [imgview setImage:image]; //set in image view

    [self performSegueWithIdentifier:@"NextView" sender:nil];
}

我尝试了下面的代码在下一个视图中显示图像,但它不起作用??

//delegate methode will be called after picking photo either from camera or library
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [self dismissViewControllerAnimated:NO completion:nil];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    NSArray *theViewControllers = [self.tabBarController viewControllers];

    //[imgview setImage:image]; //set in image view
    ImageViewController *adjViewController = (ImageViewController *)[theViewControllers objectAtIndex:0];
    [adjViewController.imageview1 setImage :image]; 

    [self performSegueWithIdentifier:@"NextView" sender:nil];        
}
4

1 回答 1

1

您可以将UIImage *image(从您的imagePickerController方法)设为全局到其视图控制器,然后将其传递给prepareForSegue.

例如:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"NextView"]) {
        ImageViewController *vc = [segue destinationViewController];
        [vc setImage:image];
    }
}

然后在你的ImageViewController.h你只需要这样的东西:

@interface ImageViewController : UIViewController {
    UIImage *image;
}
@property (nonatomic, retain) UIImage *image;

并在 .m 中合成它并将图像设置为您的imageViewin viewWillAppear。您可以阅读prepareForSegue 此处的文档。

于 2013-03-18T09:59:52.433 回答