【问题标题】:How to present UIImagePickerController or open the Camera in a Landscape-only app?如何在仅横向应用程序中呈现 UIImagePickerController 或打开相机?
【发布时间】:2015-03-23 22:08:32
【问题描述】:

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

在注册过程中,我想打开相机或展示 UIImagePickerController,但应用程序崩溃并出现以下错误:

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

如何在横向中显示图像选择器或相机?

【问题讨论】:

    标签: ios objective-c uiimage uiimagepickercontroller uiinterfaceorientation


    【解决方案1】:

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

    【讨论】:

    • 被低估了。非常感谢!
    • 是的.. 被严重低估的答案。
    【解决方案2】:

    我在 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 上运行时,它只是像普通模式视图一样全屏运行。

    【讨论】:

    • 此外,我必须继承 UIImagePicker(在横向使用时)并覆盖 var supportedInterfaceOrientations 以返回 [.landscape, .portrait](iOS 10)
    【解决方案3】:

    实现shouldAutorotate方法:

    - (BOOL)shouldAutorotate {
        UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    
        if (UIDeviceOrientationIsLandscape(interfaceOrientation)) {
            return YES;
        }
        return  NO;
    }
    

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

    【讨论】:

    • 我还没有创建 PUUIAlbumListViewController 类。应用仍然在崩溃。
    • 将上述代码sn-p粘贴到所有视图控制器中,包括PUUIAlbumListViewController。
    • 我认为 Apple 以外的很多人都无法访问 PUUIAlbumListViewController, @saif
    【解决方案4】:
    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 模拟器的四分之一部分。这是模拟器问题还是缺少什​​么?
    • 它也可以在纵向+横向模式下旋转...即使在仅支持横向的应用程序中也是如此。
    • 为什么这个解决方案有效...?这个问题没有说明同时使用 iPad/iPhone
    • 选择器视图显示后的 iPad 模拟器部分请解决这个问题我被卡住了 user1960279。
    猜你喜欢
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多