【问题标题】:Crash on presenting UIImagePickerController under iOS 6.0在 iOS 6.0 下呈现 UIImagePickerController 时崩溃
【发布时间】:2012-09-13 09:38:32
【问题描述】:

我的应用仅通过 supportedInterfaceOrientation 属性支持横向。

使用 iOS 6 之前的 iOS,我的应用可以通过 presentViewController:animated:completion: 成功加载 UIImagePickerController 的实例,即使 UIImagePickerController 本身只支持纵向。

图像选择器只是向用户展示自己。用户旋转手机,选择他们的图像,然后旋转回横向。

在 iOS 6.0 下,使用 UIImagePickerController 实例调用 presentViewController:animated:completion: 会使应用程序崩溃。我可以通过向我的supportedInterfaceOrientation 属性添加纵向选项来防止崩溃。

但是,纵向操作对我的应用来说确实没有意义。我原以为我可以使用shouldAutorotateToInterfaceOrientation 来允许应用程序“支持纵向”,但只能在这个视图中旋转到纵向。但现在该方法已被弃用,我不能使用与 shouldAutorotate 相同的技术。

有没有人知道如何在 iOS 6.0 下解决这个问题?

【问题讨论】:

  • 顺便说一句,正如我一直在调查的那样,我编译并运行了示例代码“Popovers”,它在 Xcode 4.5 和 iOS 6 模拟器下的所有方向都能完美执行,所以我认为这是错误UIImagePicker 当支持的方向掩码不是全部时。如果我能以某种方式证明这一点,计划提交一个错误 ;-)
  • 请参阅我的回答以了解此错误的解释
  • 很多人都在使用子类化 UIImagePickerController 的方案,这真的不是办法。请注意 Apple 文档,其中特别指出:The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing 请在此处使用 Apple 批准的解决方案查看我的答案:stackoverflow.com/a/12575058/662605
  • Apple 在 iOS 6.1 中修复了此错误,请查看此答案以了解解决方法:stackoverflow.com/a/12575058/662605
  • 当我的游戏开始时,当游戏中心登录对话框试图弹出时,这个答案还帮助我修复了方向崩溃。当您已经登录 GC 时,游戏永远不会崩溃。但是当您退出游戏并开始游戏时,它就崩溃了,最佳答案帮助我修复了它。

标签: orientation landscape ios6


【解决方案1】:
-(NSUInteger)supportedInterfaceOrientations
{
   return UIInterfaceOrientationMaskLandscape;
}

将解决问题,但从 iOs7 开始

【讨论】:

    【解决方案2】:

    来自 iOS 7.1 的报告:

    除了上述答案指定的内容之外,您似乎必须在 info.plist 中绝对启用纵向模式。

    如果没有这个,上面的代码/修复都不适合我。

    【讨论】:

      【解决方案3】:

      iOS 6.1 - 已修复

      从 iOS 6.1 开始,不再发生这种情况,请务必遵循我的提示以避免在 iOS 6.0.x 下崩溃,以下内容仍然适用。


      iOS 6.0.x 解决方法

      这实际上是 iOS 6.0 中的一个错误,应该在未来的 iOS 版本中修复。

      一位来自 Apple 的工程师在此处解释了此错误和解决方法:https://devforums.apple.com/message/731764

      发生这种情况是因为应用程序只需要横向,但一些 Cocoa Touch View Controller 要求严格的纵向方向,这是错误的 - 不是他们应该要求比纵向更多,而是他们对应用程序要求的解释。

      这方面的一个例子如下:

      支持横向的 iPad 应用仅显示 UIImagePickerController 通过 UIPopoverController。 UIImagePickerController 需要 纵向,但应用程序仅强制横向。错误 和...崩溃

      已报告为有问题的其他框架包括 Game Center 登录视图控制器。

      解决方法非常简单,但并不理想...您保留在 info.plist/project 信息窗格中声明的正确方向,但在 Application Delegate 类中声明您允许所有方向。

      现在,您添加到窗口的每个 View Controller 都必须指定它自己只能是 Landscape。详情请查看链接。


      我不能强调你不应该将UIImagePickerController 子类化多少,因为公认的解决方案坚持你这样做。

      这里重要的是“此类旨在按原样使用,不支持子类化。”


      在我的例子中,我将它添加到我的应用程序的委托(我有一个仅限横向的应用程序),这告诉图像选择器它可以显示,因为支持纵向:

      - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
          return UIInterfaceOrientationMaskAll;
      }
      

      然后在我恰好是UINavigationController 的视图控制器中,我包含了一个包含以下内容的类别:

      - (NSUInteger)supportedInterfaceOrientations{
          return UIInterfaceOrientationMaskLandscape;
      }
      

      现在我的应用程序没有旋转,图像选择器询问代理它是否可以显示为纵向,它被告知没关系。所以一切都很好。

      【讨论】:

      • 谢谢。为我的应用工作!干杯
      • 为了澄清,我将此方法添加到我的 AppDelegate.m -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft; }
      • 这些都不适合我。确定弹出框正确显示,但现在我的应用程序将旋转到它喜欢的任何方向,包括我绝对不想要的肖像。我该怎么做才能解决这个问题?
      • 听起来您没有正确设置导航控制器提示。应用程序委托会说所有旋转都可以,但是您的根视图控制器(您的容器控制器)必须将其限制为仅横向。如果您的应用正在旋转所有方向,那么这是您做错的最后一部分。
      【解决方案4】:

      虽然子类化 UIImagePickerController 有效,但类别是更好的解决方案:

          @implementation UIImagePickerController (NonRotating)
      
          - (BOOL)shouldAutorotate
          {
              return NO;
          }
      
          -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
          {
              return UIInterfaceOrientationPortrait;
          }
      
          @end
      

      【讨论】:

      • 为什么这个答案被否决了?在与我的仅纵向模式应用程序略有不同的情况下,我发现添加一个类别: [at]implementation UINavigationController(landscape) - (BOOL)shouldAutorotate { return YES; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeRight|UIInterfaceOrientationMaskLandscapeLeft; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationLandscapeRight; [at]end 到我的应用程序委托,然后选择摘要下的所有方向,它修复了它。
      【解决方案5】:

      我遇到了类似的问题,但在 iPad 横向应用程序中。我在弹出窗口中展示了图像选择器。它在 iOS 6 下崩溃。错误表明选择器想要纵向,但应用程序只提供横向视图,而且......重要的是......选择器的 shouldRotate 返回 YES。

      我将此添加到创建选择器的 ViewControllerClass.m 中

      @interface NonRotatingUIImagePickerController : UIImagePickerController
      
      @end
      
      @implementation NonRotatingUIImagePickerController
      
      - (BOOL)shouldAutorotate
      {
          return NO;
      }
      
      @end
      

      然后改用那个类

      UIImagePickerController *imagePicker = [[NonRotatingUIImagePickerController alloc] init];
      [myPopoverController setContentViewController:imagePicker animated:YES];
      

      这为我解决了问题。你的情况有点不同,但听起来基本上是相同的错误。

      【讨论】:

      • 非常适合我。非常感谢!
      • 我遇到了同样的问题,所以对问题投赞成票,对答案投赞成票!你为你的 supportedInterfaceOrientationsForWindow: 设置了什么?我正在返回 UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight 但这导致我的应用程序以纵向启动! Info-Plist 显示左侧横向和右侧横向
      • FIXED 通过返回 UIInterfaceOrientationMaskLandscape 并将 shouldAutorotate 设置为返回 NO,我的应用程序现在以横向方式启动,并且呈现图像选择器的视图控制器不会崩溃。
      • 子类化 UIImagePickerController 真的不是这样做的方法。请注意 Apple 文档,其中特别指出:The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing 请在此处使用 Apple 批准的解决方案查看我的答案:stackoverflow.com/a/12575058/662605
      • 不要打扰 UIImagePickerController -- 只需使用一个类别来实现相同的结果,它也更容易。 stackoverflow.com/a/12834354/138905
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      • 2015-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-02
      相关资源
      最近更新 更多