解雇相机视图会导致应用崩溃

时间:2012-12-17 22:48:05

标签: iphone ios xcode uiimagepickercontroller

当我尝试关闭我的UIImagePickerController时,它会崩溃应用程序。错误是:“因未捕获的异常而终止应用程序'UIApplicationInvalidInterfaceOrientation',原因:'preferredInterfaceOrientationForPresentation必须返回支持的接口方向!'”

我在视图控制器中设置了首选的界面方向。

-(NSUInteger)supportedInterfaceOrientations
{
 return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}

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

- (BOOL) shouldAutorotate {
return YES;
}

这是我打电话来调出相机的方法,这对于添加相机效果很好,但就像我说的那样,当我尝试移除相机时崩溃了。

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

5 个答案:

答案 0 :(得分:1)

好的,找到了解决方案,它非常简单,我只是将我的背面方法更改为:

    [self dismissModalViewControllerAnimated:YES];

现在我的相机消失了,当我按下后退按钮时,我可以看到原始视图。 我仍然没有弄清楚是什么导致了原始问题,因为我已经浏览了info.plist和支持方向的方法,但这实现了我想要的。

如果有人有任何想法,我仍然很好奇导致错误的原因。

答案 1 :(得分:1)

您可以尝试删除此方法:

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

它会修复它。但有时它会导致降低stateBar高度。

答案 2 :(得分:0)

您无法从超级视图中删除UIViewController的主视图。

而不是:

self.view = ar_overlayView;

试试这个:

[self.view addSubview:ar_overlayView];

然后您就可以正确地从超视图中删除它。

答案 3 :(得分:0)

你应该使用类似于下面的didFinishPickingMedieWithInfo方法并使用[self dismissModalViewControllerAnimated:YES];

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

{

if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {

    NSLog(@"Camera");


}

else {


    NSLog(@"Album");

 }

 [self dismissModalViewControllerAnimated:YES];
 }

答案 4 :(得分:0)

如果是相机覆盖,我认为您不需要自己将ar_overlayView添加到当前视图中。

以下是您的代码现在正在做的事情:

  1. ar_overlayView添加到当前视图
  2. ar_overlayView添加为相机的叠加视图
  3. 显示摄像机视图(模态)
  4. 此时,ar_overlayView正在显示两次。当您在解除摄像机视图时发送removeFromSuperview消息时,它可能会变得混乱,因为它同时处于两个视图层次结构中。

    跳过self.view = ar_overlayView;[self.view addSubview:ar_overlayView];行可以解决问题。

    此外,dismissViewControllerAnimated:completion:应发送到presentViewController:animated:completion被调用的同一对象(在这种情况下为self):

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