UIPickerView UIImage and Segue

2019-09-08 20:03发布

问题:

In my app, I use the camera with UIImagePickerCollection in my UserView. But once the user, select "use photo" I would like to have my photo in an other view which is "cameraView" because I have some treatment to do with. Here is my code :

-(void)takeAPicture{
NSLog(@"CHEESE");
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];

imagePicker.delegate = self;

imagePicker.sourceType =
UIImagePickerControllerSourceTypeCamera;

imagePicker.mediaTypes =
@[(NSString *) kUTTypeImage];

imagePicker.allowsEditing = NO;
[self presentViewController:imagePicker
                   animated:YES completion:nil];
}

-(void)imagePickerController:
(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self dismissViewControllerAnimated:YES completion:^{
    _img = [info objectForKey:UIImagePickerControllerOriginalImage];
    if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera) {
        UIImageWriteToSavedPhotosAlbum(_img, nil, nil, nil);
    }}];
    [self performSegueWithIdentifier:@"toCameraView" sender:self];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:@"toCameraView"])
        {
            CameraViewController *transferViewController = segue.destinationViewController;
        transferViewController.img = _img;
    }
}

When i'm running my app, I receive an warning message which is :

Warning: Attempt to present <CameraViewController: 0x17e20eb0> on <EIUserViewController: 0x189b1000> while a presentation is in progress!

I don't understand what it is, and how to deal with it, I can't manage to receive my image in the other view and showing my view directly after choosing "use picture". If someone has an explanation or a solution ? thank you guys !

回答1:

You are trying to [self performSegueWithIdentifier:@"toCameraView" sender:self] during UIImagePickerController dismissing, other words: cannot animate second VC appearing during first VC disappearing. Try to move [self performSegueWithIdentifier:@"toCameraView" sender:self]; inside completionBlock:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self dismissViewControllerAnimated:YES completion:^{
    _img = [info objectForKey:UIImagePickerControllerOriginalImage];
    if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera) {
        UIImageWriteToSavedPhotosAlbum(_img, nil, nil, nil);
    [self performSegueWithIdentifier:@"toCameraView" sender:self];
    }}];
}


回答2:

So, when the imagePicker calls the delegate method imagePickerController: didFinishPickingMediaWithInfo: the imagePicker is still being presented by your viewController.

One solution to this would be presenting the cameraView when you come back to your viewController like so:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self dismissViewControllerAnimated:YES completion:^{
    _img = [info objectForKey:UIImagePickerControllerOriginalImage];
    if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera) {
        UIImageWriteToSavedPhotosAlbum(_img, nil, nil, nil);
    }}];
    self.newPicture = YES;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (self.newPicture) {
        [self performSegueWithIdentifier:@"toCameraView" sender:self];
        self.newPicture = NO;
    }
}

Hope it helps