【问题标题】:How do I display a UIImage from one ViewController in a second ViewController?如何在第二个 ViewController 中显示来自一个 ViewController 的 UIImage?
【发布时间】:2012-10-03 12:01:23
【问题描述】:

所以我开始认为这会很简单,但我真的不知道该怎么做!基本上我使用的是 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 中弹出的照片库中选择 UIImage 时,我希望能够在 ViewController 2 中显示 UIImage

【问题讨论】:

    标签: uiviewcontroller uiimageview uiimage uitabbarcontroller uiimagepickercontroller


    【解决方案1】:

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

        newView = [[UIView alloc]init] ;
    
        [newView addSubView:image1] ;
    

    然后:

        [self.VC1.view addSubView:newView] ;
    

    或:

        [self.VC1 setView:newView] ;
    

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

    【讨论】:

    • 酷,我会试试看,然后回复你。感谢您的回复,除非我亲自向某人展示,否则我总是很难解释这些事情,但感谢您的建议!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    相关资源
    最近更新 更多