如何在仅限景观的应用程序中呈现UIImagePickerController或打开相机?

时间:2015-01-25 07:12:08

标签: ios objective-c uiimage uiimagepickercontroller uiinterfaceorientation

我正在创建一个仅在横向模式下工作的应用程序。

在注册期间,我想打开相机或呈现UIImagePickerController,但应用程序崩溃时出现以下错误:

*** Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and [PUUIAlbumListViewController shouldAutorotate] is returning YES'

如何在风景中呈现图像选择器或相机?

4 个答案:

答案 0 :(得分:6)

I found solution, case closed. i add in my appdelegate.m:

-(NSUInteger)application:(UIApplication *)application 
    supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        return UIInterfaceOrientationMaskAll;
    else  /* iPad */
        return UIInterfaceOrientationMaskAllButUpsideDown;
}

答案 1 :(得分:4)

我在iOS 9中遇到了同样的问题,我找到的解决方案(这里或关于SO的类似问题)都没有为我工作。但是,我找到了一个使用iOS 8中引入的UIPopoverPresentationController的解决方案。这就是我正在做的事情,在iPhone上仅限肖像的应用程序中,以及仅在iPad上使用的横向应用程序。在我的应用代表中,我有这个:

  func application(application: UIApplication,
      supportedInterfaceOrientationsForWindow window: UIWindow?)
      -> UIInterfaceOrientationMask {
    if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
      return .Landscape
    } else {
      return .Portrait
    }
  }

然后在需要显示照片选择器的视图控制器中:

@IBAction func choosePictureFromLibrary() {
    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self

    // Set the presentation style and grab the presentation controller,
    // which is created for you.
    imagePickerController.modalPresentationStyle = .Popover
    let presentationController = imagePickerController.popoverPresentationController

    // You must set a sourceView or barButtonItem so that it can 
    // draw a "bubble" with an arrow pointing at the right thing.
    presentationController?.sourceView = photoButton

    self.presentViewController(imagePickerController, animated: true) {}
  }

使用.Popover模式演示样式可以在iPad上覆盖屏幕,显示一个漂亮,紧凑(基本上是iPhone大小)的视图。当你在iPhone上运行时,它就像普通的模态视图一样全屏运行。

答案 2 :(得分:4)

适用于iPhone和iPad的Swift 3解决方案

func openGallary(_ sender:UIButton)
    {
        imagepicker?.delegate = self
        imagepicker?.allowsEditing = false
        imagepicker!.sourceType = UIImagePickerControllerSourceType.photoLibrary
        if UIDevice.current.userInterfaceIdiom == .phone
        {
            self.present(imagepicker!, animated: true, completion: nil)
        }
        else
        {

            imagepicker?.modalPresentationStyle = .popover
            let presentationController = imagepicker?.popoverPresentationController
            // You must set a sourceView or barButtonItem so that it can
            // draw a "bubble" with an arrow pointing at the right thing.
            presentationController?.sourceView = sender
            self.present(imagepicker!, animated: true) {}
        }
    }

答案 3 :(得分:-4)

实施shouldAutorotate方法:

- (BOOL)shouldAutorotate {
    UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];

    if (UIDeviceOrientationIsLandscape(interfaceOrientation)) {
        return YES;
    }
    return  NO;
}

This image shows the landscape orientation checked, you need to do the same

将此代码添加到PUUIAlbumListViewController和所有其他视图控制器。确保您仅在目标中选中了横向(横向左侧和右侧)。