我在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];
}
答案 0 :(得分: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中合成它并将图像设置为imageView
中的viewWillAppear
。您可以阅读prepareForSegue
here的文档。