【问题标题】:UIImagePickerController to pick from Album in landscape only iPhone/iPad appUIImagePickerController 在仅横向 iPhone/iPad 应用程序中从相册中选择
【发布时间】:2016-11-15 18:28:48
【问题描述】:

我在谷歌上搜索时发现了很多文章,但似乎没有一个真正涵盖这个案例,所以我会尽量具体。 我有一个适用于 iPhone 和 iPad 的仅限横向应用程序。我试图让用户使用以下代码从他的相册中挑选一张图片:

let imagePicker = UIImagePickerController()
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.modalPresentationStyle = UIModalPresentationStyle.Popover
imagePicker.allowsEditing = false
context.presentViewController(imagePicker, animated: true, completion: {
    () -> Void in

})

我正在使用 Popover 演示样式,因为 Apple mentions 在 iPad 上必须这样设置,否则将引发异常:

该表表明,在 iPad 上,如果您指定源类型 photoLibrary 或 savedPhotosAlbum,您必须出示图片选择器 使用 popover 控制器(要了解如何执行此操作,请参阅 UIPopoverPresentationController)。如果您尝试呈现图像 选择器模态(全屏)用于在保存的图片中进行选择和 电影,系统会引发异常。

失败并出现以下错误

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

我尝试过的:

  • 子类 UIImagePickerController 和 UIViewController 以支持横向方向并将 shouldAutorotate 设置为 false,但我认为这没有用,因为这里失败的类是 PUUIAlbumListViewController
  • 将我的应用设置为支持人像,这可行,但随后整个应用会自动使用手机,这不会发生
  • 使用中间的 UIPopoverPresentationController 但从来没有让它显示任何东西

更新 不是答案,但尽管看起来很疯狂,但我认为这是不可行的。感谢 Apple 提供了最先进的 API,其中一切都有意义。 我最终使用了DKImagePickerController,完美地完成了这项工作

【问题讨论】:

    标签: ios swift uiimagepickercontroller uipopovercontroller landscape


    【解决方案1】:

    我在使用强制横向的应用程序时也遇到了这个问题。唯一的选择是缓和并允许图像选择器以肖像的形式出现,因为这是其呈现时的默认位置。

    【讨论】:

      【解决方案2】:

      子类化您的导航控制器并更改为横向掩码。然后从 NavigationController 显示您的 Image Picker 控制器。

      class CustomNavigation: UINavigationController {
      override func viewDidLoad() {
          super.viewDidLoad()
      
          // Do any additional setup after loading the view.
      }
      
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
      override var shouldAutorotate: Bool {
          return false
      }
      override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
          return UIInterfaceOrientationMask.portrait
      }
      
      /*
      // MARK: - Navigation
      // In a storyboard-based application, you will often want to do a little preparation before navigation
      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
          // Get the new view controller using segue.destinationViewController.
          // Pass the selected object to the new view controller.
      }
      */
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-23
        • 1970-01-01
        • 2010-10-06
        • 1970-01-01
        • 1970-01-01
        • 2014-03-12
        • 2014-10-25
        • 2011-05-27
        相关资源
        最近更新 更多