简体   繁体   English

无法从另一个视图控制器设置UIImageView图像

[英]Trouble setting UIImageView image from another view controller

The first view controller calls this method that sends the image 第一个视图控制器调用此方法发送图像

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

...
[wallPostCreateViewController viewDidLoad];

  [wallPostCreateViewController selectImage:wallPostCreateViewController.myImage];

NSLog(@"FROM PAWWallViewController %@",wallPostCreateViewController.myImage); //Image is there


}

I receive the image in the second view controller but it fails to put the image into its UIImageView. 我在第二个视图控制器中收到图像,但是它无法将图像放入其UIImageView中。

- (void)selectImage:(UIImage *)img
{

if (self.condition)
{
    NSLog(@"View loaded!"); //is loaded
    [self selectImageNow:img];
    self.condition = NO;
}
else
    NSLog(@"View did NOT load!");


}
- (void)selectImageNow: (UIImage *)img
{

    NSLog(@"Image passed  %@", img); // This is the UIImage being passed.

    self.imageView = [[UIImageView alloc] initWithImage:img];
    self.imageView.image = img;

    NSLog(@"Image in view  %@", self.imageView.image); //has value
    NSLog(@"Imageview  %@", self.imageView); //has value


 }

UPDATED: 更新:

The code above is updated now. 上面的代码现在已更新。 All of the logs now have values but I still see no image or image view when I run the app 现在,所有日志都有值,但是运行应用程序时我仍然看不到任何图像或图像视图

UIImage object从一个UIImage object发送到另一个ViewController然后将该UIImage分配给SecondController ViewWillAppearViewDidLoad方法中的UIImageView

The issue seems like your imgView is nil. 问题似乎您的imgView为零。 So make sure that your method should not get called, before your view has been loaded. 因此,请确保在加载视图之前不要调用您的方法。

I think you need to set the imageView of first view controller in prepareForSegue method that is in the second view controller, where you will have your destination to be the first view controller, do something like this in the second view controller: 我认为您需要在第二个视图控制器中的prepareForSegue方法中设置第一个视图控制器的imageView,在该方法中,您的目的地将成为第一个视图控制器,在第二个视图控制器中执行以下操作:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if([segue.identifier isEqualToString:@"firstVwCtrl"]){

    FirstViewController *frstVwCtrl = [segue destinationViewController];
    [frstVwCtrl setImgGot:_photoView.image];

          }  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM