如何在只支持横屏的应用程序中呈现UIImagePickerController或打开相机?

11

我正在创建一个只能在横屏模式下运行的应用程序。

在注册时,我想打开相机或呈现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个回答

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

解决方案很好,但我看到的是崩溃已经消失了,但在选择器视图显示后,应用程序只在iPad模拟器的四分之一部分中显示。这是模拟器问题还是缺少某些东西? - Yogesh Lolusare
它还可以在纵向和横向模式下旋转...即使是仅支持横向的应用程序。 - shaqir saiyed
为什么这个解决方案有效?问题并没有提到它是否适用于iPad/iPhone。 - fatuhoku
iPad模拟器上选择器视图显示后的部分,请解决这个问题,我卡住了。用户1960279。 - Pooja Srivastava

6

适用于 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) {}
        }
    }

1
非常被低估。非常感谢你! - LinusGeffarth
是的,这个答案被高度低估了。 - Talib Shabbir Hussain

4

我曾在iOS 9上遇到同样的问题,但是我找到的所有提议解决方案(无论是在这里还是在SO上的类似问题)都不适用于我。但是,我发现了一种使用UIPopoverPresentationController的解决方案,该控制器在iOS 8中引入。在我的应用程序中,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 上运行时,它就像普通的模态视图一样全屏运行。

此外,我还需要对UIImagePicker进行子类化(在横向使用时),并覆盖var supportedInterfaceOrientations以返回[.landscape, .portrait](iOS 10)。 - Matej Vargovčík

-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和所有其他视图控制器。确保在目标设置中仅选择了横屏模式(向左和向右)。

我还没有创建PUUIAlbumListViewController类,应用程序仍然崩溃。 - user1960279
将上述代码片段粘贴到所有视图控制器中,包括PUUIAlbumListViewController。 - Saif
3
我认为除了苹果内部的人员之外,很少有人能够访问PUUIAlbumListViewController。 @saif - Lucas van Dongen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接