Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DIsmissing a UIImagePickerController is also making the parent dissapear

I'm using the following code to show a UIImagePickerController to take a picture:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setDelegate:self];    
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:imagePicker animated:YES completion:nil];

Note: self is a UIViewController embedded inside a Container View, which itself takes part in a UINavigationController

And I have also implemented the following methods:

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];  
    [self.imgProfile setImage:image];   
    [self dismissViewControllerAnimated:YES completion:nil];    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

When I either choose the picture taken or cancel it, the UIImagePickerController goes away, but also the main view disappears! With a Fade to Black.

I'm coding for iOS7 on an iPad only app, in case it has anything to do with it.

Here is a video that shows the problem. Excuse the blur, but it is under an NDA.

http://www.youtube.com/watch?v=sIaPyRlIqyE

like image 532
Jan Avatar asked Nov 30 '22 00:11

Jan


1 Answers

I've encountered the same issue under iOS7, and the only solution I've found is do to as follows;

When presenting the picker from your VC ;

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];

[self addChildViewController:picker] ;

[picker didMoveToParentViewController:self] ;

[self.view addSubview:picker.view] ;

Then, when handling the picker delegates - instead of dismissModal, use ;

[picker.view removeFromSuperview] ;
[picker removeFromParentViewController] ;

All you lose is the animated present/dismiss - other than that, all should be well.

like image 59
Malcolm Sparrow Avatar answered Dec 06 '22 00:12

Malcolm Sparrow