如何在第二个ViewController中从一个ViewController显示UIImage?

时间:2012-10-03 12:01:23

标签: uiviewcontroller uiimageview uiimage uitabbarcontroller uiimagepickercontroller

所以我开始认为这将是非常直接但我真的无法弄清楚如何做到这一点!基本上我使用的是TabBar应用程序,因此每个页面都由UITabBarController管理。我创建了一个UIImage,当您从照片库中选择它时显示,我希望该选择显示在不同的视图控制器中。这是我在第一个VC中的代码:

- (void)addNew:(id)sender {
    NSLog(@"Clicked");
    UIImagePickerController *controller = [[UIImagePickerController alloc] init];
    controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentModalViewController:controller animated:YES];
    [controller setDelegate:self];

    [self.photoView.view removeFromSuperview];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage : (UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    NSLog(@"this function has started");
    [self.view setAlpha:1.0f];
    imageV1 = [[UIImageView alloc] initWithImage:image];
    imageV1.frame = CGRectMake(10, 200, 100, 100);
    image1 = image;
    [imageV1 setImage:image];
    [scrollView addSubview:imageV1];

    [self dismissModalViewControllerAnimated:YES];
}

这样就可以在这个视图中显示图像(这不是我想要的)。我希望它在此视图中显示:

- (void)viewDidLoad
{
    [scrollView setScrollEnabled:YES];
    [scrollView setContentSize:CGSizeMake(320, 500)];

    imageV2 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 50, 100, 100)];
    imageV2.backgroundColor = [UIColor blueColor];
    self.imageV2.image = VC1.imageV1.image;
    [imageV2 setImage:VC1.image1];
[scrollView addSubview:imageV2];



    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

我通过创建VC1实例链接了两个VC

ViewControllerOne *VC1;

@property (nonatomic, retain) ViewControllerOne *VC1;

我希望我已经很好地解释了这一点,但总结一下,当从ViewController 1中弹出的photolibrary中选择UIImage时,我希望能够在ViewController 2中显示UIImage

1 个答案:

答案 0 :(得分:1)

我不确定究竟哪个VC是哪个以及每个VC的声明位置,但我认为你说的是​​你的初始VC中有一个图像和一个新声明的VC。如果是这种情况,我会在您的初始VC中使用您的UIImage创建一个视图,然后将其添加为新VC的视图或子视图。

    newView = [[UIView alloc]init] ;

    [newView addSubView:image1] ;

然后:

    [self.VC1.view addSubView:newView] ;

或:

    [self.VC1 setView:newView] ;

很抱歉,如果我误解了您的问题。也许尝试标记哪个VC哪个更好并保持一致?