简体   繁体   中英

Dismissing camera view crashes app

When I try to dismiss my UIImagePickerController, it crashes the app. the error is: "Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'preferredInterfaceOrientationForPresentation must return a supported interface orientation!'"

I have the preferred interface orientation set in my view controller.

-(NSUInteger)supportedInterfaceOrientations
{
 return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}

- (BOOL) shouldAutorotate {
return YES;
}

Here is the method I'm calling to bring up the camera, this works fine for adding the camera, but like I said, crashes when I try to remove the camera.

-(IBAction)addCamera:(id)sender
{

self.cameraController = [[UIImagePickerController alloc] init];
self.cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;

self.cameraController.cameraViewTransform = CGAffineTransformScale(self.cameraController.cameraViewTransform,
                                                                   1.13f,
                                                                   1.13f);

self.cameraController.showsCameraControls = NO;
self.cameraController.navigationBarHidden = YES;

self.wantsFullScreenLayout = YES;

ar_overlayView = [[UIView alloc] initWithFrame:CGRectZero];

self.view = ar_overlayView;
[self.cameraController setCameraOverlayView:ar_overlayView];
[self presentViewController:cameraController animated:NO completion:nil];
[ar_overlayView setFrame:self.cameraController.view.bounds];
}

-(IBAction)back:(id)sender
{
[ar_overlayView removeFromSuperview];
[cameraController dismissViewControllerAnimated:NO completion:nil];
}

Alright, found the solution, it was really simple, I just changed my back method to:

    [self dismissModalViewControllerAnimated:YES];

Now my camera goes away and I can see my original view when I press the back button. I still haven't figured out what was causing the original problem as I've gone through the info.plist and the methods for supported orientations, but this accomplishes what I wanted.

I'm still curious as to what was causing the error though if anyone has any ideas.

You can try remove this method:

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

It will fix it.but sometimes it will lead to reduce stateBar height.

You cannot remove a UIViewController's main view from its superview.

Instead of this:

self.view = ar_overlayView;

Try this:

[self.view addSubview:ar_overlayView];

Then you will be able to remove it from the superview correctly.

You should be using the didFinishPickingMedieWithInfo method similar to below and use [self dismissModalViewControllerAnimated:YES];

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

{

if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {

    NSLog(@"Camera");


}

else {


    NSLog(@"Album");

 }

 [self dismissModalViewControllerAnimated:YES];
 }

I don't think you need to add ar_overlayView to the current view yourself if it's a camera overlay.

Here's what your code is doing now:

  1. Add ar_overlayView to the current view
  2. Add ar_overlayView as the camera's overlay view
  3. Show the camera view (modal)

At this point, ar_overlayView is being displayed twice . When you send it the removeFromSuperview message on dismissing the camera view, it might be getting confused since it's in two view hierarchies at the same time.

Skipping the self.view = ar_overlayView; or [self.view addSubview:ar_overlayView]; lines should fix the problem.

Also, dismissViewControllerAnimated:completion: should be sent to the same object that presentViewController:animated:completion was called on ( self in this case):

-(IBAction)addCamera:(id)sender
{
  // -- snip -- //
  ar_overlayView = [[UIView alloc] initWithFrame:self.cameraController.view.bounds];
  [self.cameraController setCameraOverlayView:ar_overlayView];
  [self presentViewController:self.cameraController animated:NO completion:nil];
}

-(IBAction)back:(id)sender
{
  [self dismissViewControllerAnimated:NO completion:nil];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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