2

Here my code :

@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];

        }];
    }

And my perform segue is below :

 - (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;
        }

    }

I am getting null image only.My image is not coming.What the issue i am doing here ?

Please help me out

UPDATED :

My updated code, work like - giving me console message like :

its null dataa - post vc its not an null image - prepareForSegue method

Not able to find out why ist happening.

     - (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"];
        }


    }
  • I suggest you to use this link https://stackoverflow.com/questions/31576232/passing-image-to-another-view-controller-swift – Vinaykrishnan May 24 '18 at 09:04
  • Your view controller, that holds the image, is dismissed in the completion block. Try to put this: UIImage * pickedImage = self->_chosedImage; one line above. – Cristian May 24 '18 at 09:06
  • @Cristian i am getting thsi warning `iincompatible pointer types initializing 'UIImage *' with an expression of type 'UIImageView *'` – chandru kin May 24 '18 at 09:16
  • @Cristian check my updated one...its not shoing the image – chandru kin May 24 '18 at 09:18
  • Please show us code of PostVC of Sourcemage variable – Arnab May 24 '18 at 09:22
  • @Arnab Hore i have added.Please check it out – chandru kin May 24 '18 at 09:27
  • @chandrukin That warning was because you were passing an UIImageView to an UIImage, but I see that you figured that out. There may be a problem with your _PostImageView, you can check its frame by displaying it in the console, or make sure you don't have other view that covers your _PostImageView, by having it displayed above _PostImageView. – Cristian May 24 '18 at 10:39
  • @chandrukin If you just put this line in your viewWillAppear: _PostImageView.image = [UIImage imageNamed:@"Travel.png"]; does the image show? (assuming that you have that image in your xcode project) . If it shows, then most likely it's not a problem here, but in your initial controller, from where you are initially passing the image. If it doesn't show, then most likely there is a problem with your _PostImageView, and you will have to check its frame, position and hierarchy. – Cristian May 24 '18 at 10:56
  • yes, on else statement that image is showing if its null – chandru kin May 24 '18 at 10:57
  • @chandrukin If you get rid of the [self dismissViewControllerAnimated:YES completion:^{ ..... }]; line and just keep performSegueWithIdentifier what do you get? Also seems odd that you are dismissing a controller and performing a segue right after. Try to get rid of this approach, let only the performSegueWithIdentifier and see what you get. – Cristian May 24 '18 at 11:07

3 Answers3

0

Is the variable Sourcemage in your destination viewcontroller(PostVC) is UIImage?

Please check whether the property is UIImage or not.

0

This is because you are passing UIImageView instead of UIImage

Update your code as below,

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

in above code, you must pass UIImage not UIImageView itself and handle it like this,

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    PostVC *vc = (PostVC*)segue.destinationViewController;
    UIImage *img = (UIImage*)sender;
    vc.Sourcemage = img;
}
PPL
  • 6,357
  • 1
  • 11
  • 30
0

Why don't you just do like this?

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