UIImage和prepareforSegue无法传递图像

时间:2018-05-24 08:55:27

标签: ios objective-c uiimageview

这是我的代码:

@property (weak, nonatomic) IBOutlet UIImageView *chosedImage;

    - (IBAction)cropImageTap:(id)sender {

        [self dismissViewControllerAnimated:YES completion:^{
             UIImage * pickedImage = self->_chosedImage;
            //[UIImage imageNamed:@"helloWorld.jpg"];

            [self performSegueWithIdentifier:@"gotomainimage" sender:pickedImage];

        }];
    }

我的表演如下:

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


        PostVC *vc = (PostVC*)segue.destinationViewController;
        UIImage *img = (UIImage*)sender;

        if (img.images == nil) {
            NSLog(@"null img");
             vc.Sourcemage = img;
        }
        else {

             NSLog(@"not null img");
             vc.Sourcemage = img;
        }

    }

我只得到空图像。我的图像没有来。我在这里做的问题是什么?

请帮帮我

更新:

我的更新代码,工作方式 - 给我控制台消息,如:

其null dataa - post vc  它不是空图像 - prepareForSegue方法

无法找出原因。

     - (IBAction)cropImageTap:(id)sender {


        UIImageView * pickedImage = _chosedImage;
        [self dismissViewControllerAnimated:YES completion:^{
            [self performSegueWithIdentifier:@"gotomainimage" sender:pickedImage.image];
        }];
    }

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

        PostVC *vc = (PostVC*)segue.destinationViewController;
        UIImage *img = (UIImage*)sender;

        if (img != nil) {
             NSLog(@"its not an null image");
            vc.Sourcemage = img;

        } else {
             NSLog(@"its an null image");
        }
    }

in my postvc :

    @property(nonatomic) UIImage *Sourcemage;
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];



        if (self.Sourcemage != nil){
            NSLog(@"not null dataa");

            _PostImageView.image = _Sourcemage;

        } else {
    NSLog(@"not null dataa");
           _PostImageView.image = [UIImage imageNamed:@"Travel.png"];
        }


    }

3 个答案:

答案 0 :(得分:0)

目标viewcontroller(PostVC)中的变量Sourcemage是UIImage吗?

请检查该属性是否为UIImage。

答案 1 :(得分:0)

这是因为您传递 UIImageView 而不是 UIImage

更新您的代码,如下所示

UIImageView * pickedImage = _chosedImage;
[self dismissViewControllerAnimated:YES completion:^{
   [self performSegueWithIdentifier:@"gotomainimage" sender:pickedImage.image];
}];

在上面的代码中,您必须传递 UIImage 而不是 UIImageView 本身并按此处理,

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    PostVC *vc = (PostVC*)segue.destinationViewController;
    UIImage *img = (UIImage*)sender;
    vc.Sourcemage = img;
}

答案 2 :(得分:0)

你为什么不这样做?

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    PostVC *vc = (PostVC*)segue.destinationViewController;
    vc.Sourcemage = _checkedImage;
}