【问题标题】:Application freezes when trying to present rear camera on iPad尝试在 iPad 上显示后置摄像头时应用程序冻结
【发布时间】:2017-05-06 12:11:41
【问题描述】:

我正在开发的应用程序可以在 iPhone 和 iPad 上运行。该应用程序具有的功能之一是从相机捕获图像。我正在使用 UIImagePickerController 来实现此功能。这是我的代码块;

self.imagePicker.sourceType = .camera            
self.imagePicker.cameraCaptureMode = .photo
self.present(self.imagePicker, animated: true, completion: nil)

该应用程序的工作方式与它在 iPhone 中的设计相同,但是当我在 iPad 上运行相同的代码时,应用程序会冻结。此问题仅发生在 iPad 中,但仅适用于后置摄像头。如果我从图像选择器中选择前置摄像头,应用程序会启动摄像头,但当我按下切换摄像头按钮时它会冻结。

据我了解,当应用程序尝试绘制相机时,会出现日志问题。

日志:

2016-12-20 20:10:33.708816 Ronin[681:148977] CGContextAddPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Dec 20 20:10:33  Ronin[681] <Error>: CGContextAddPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
2016-12-20 20:10:33.708925 Ronin[681:148977] clip: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Dec 20 20:10:33  Ronin[681] <Error>: clip: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
2016-12-20 20:10:33.708991 Ronin[681:148977] CGContextSetFillColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Dec 20 20:10:33  Ronin[681] <Error>: CGContextSetFillColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
2016-12-20 20:10:33.709047 Ronin[681:148977] CGContextFillRects: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Dec 20 20:10:33  Ronin[681] <Error>: CGContextFillRects: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

我用于测试的 iPad 是:iPad Air 2 - iOS 10.2

编辑

UIImagePickerController.isSourceTypeAvailable(.camera) 代码块为我的测试用例返回 true。我已经添加了这个控件,但之前没有提到。

我还注意到,当应用程序被冻结时,内存消耗开始增加,同时应用程序由于使用太多内存而崩溃。

此外,我创建了一个空项目并实施了相同的方法来捕获图像,这与空项目中的设计相同。在这一点上,我认为这个问题可能与某些项目设置有关。

编辑 - 2

我为 CGPostError 添加了符号断点,这里是堆栈跟踪:

似乎发送到 UIProgressView 的无效上下文是问题的原因。

我们将不胜感激。

编辑 - 3

正如我之前提到的,我确实检查了代码块中的 UIImagePickerController.isSourceTypeAvailable(.camera),该问题与相机可用性无关。当我以模态方式展示它时,相机也可以作为弹出框使用。

【问题讨论】:

  • 你是在主线程上调用你的代码吗?在您的堆栈跟踪屏幕截图中,我在主线程上没有看到您的代码痕迹。
  • 是的,实际上我展示了带有导航按钮的图像选择器点击@MihaiFratu
  • 会不会是全系统iOS版本相关问题,也就是说iPad Air 2 iOS 10.2固件有bug?
  • 我不这么认为,因为我创建了一个空项目来测试我的代码和固件,但代码在空项目中工作。所以我认为某些项目设置可能是问题背后的原因@CihanKöseoğlu

标签: ios swift ipad camera


【解决方案1】:

终于解决了问题,

我为所有应用程序实现了一个主题方法。在我的 AppDelegate 文件中,我在初始化应用程序时设置了用户界面设置。

问题来了,

相机 UI 中有一个 UISlider,此滑块视图在 iPad 后置摄像头中始终可见,但在 iPhone 中,它仅在您使用缩放时可见。

    UISlider.appearance().minimumTrackTintColor = themeUI.PrimaryColor.withAlphaComponent(100)
    UISlider.appearance().thumbTintColor = themeUI.PrimaryColor

这两行改变了应用程序中所有滑块的外观,这意味着它也改变了相机滑块的 UI。

正如您在我授予的屏幕截图中看到的那样,在绘制相机 CoreGraphics 库调用时

    [UISlider setMinimumTrackTintColor];

不知何故为滑块设置 minimumTrackTintColor 导致无效上下文错误。

另外设置 thumTintColor 工作正常,它也改变了相机中 UISlider 的拇指颜色:)

也许这个问题与 Swift 3 有关,我会报告一个错误,我们会看到:)

【讨论】:

  • 谢谢!这为我们解决了本月最奇怪的 UIKit 问题之一。
【解决方案2】:

用这个替换你的代码:

斯威夫特 3

   if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)
    {
        let imagePicker:UIImagePickerController = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera
        imagePicker.allowsEditing = true
        self.present(imagePicker, animated: true, completion: nil)
    }
    else
    {
        let alert:UIAlertController = UIAlertController(title: "Camera not available", message: "Unable to find a camera on this device", preferredStyle: UIAlertControllerStyle.alert)
        self.present(alert, animated: true, completion: nil)
    }

斯威夫特 2

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
     {
          let imagePicker:UIImagePickerController = UIImagePickerController()
          imagePicker.delegate = self
          imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
          imagePicker.allowsEditing = true
         self.presentViewController(imagePicker, animated: true, completion: nil)
     }
     else
     {
          let alert:UIAlertController = UIAlertController(title: "Camera not available", message: "Unable to find a camera on this device", preferredStyle: UIAlertControllerStyle.Alert)
          self.presentViewController(alert, animated: true, completion: nil)
     }

【讨论】:

    【解决方案3】:

    设置环境变量的方法如下:https://stackoverflow.com/a/31874419/3724800

    这是我用来在通用应用中打开相机的代码:

    if UIImagePickerController.isSourceTypeAvailable(.camera) {
                let imagePicker = UIImagePickerController()
                imagePicker.delegate = self
                imagePicker.sourceType = .camera;
                imagePicker.allowsEditing = false
                present(imagePicker, animated: true, completion: nil)
    }
    

    【讨论】:

      【解决方案4】:

      您应该检查相机是否可用:

      if UIImagePickerController.isSourceTypeAvailable(.camera) {
          // Implement UIImagePickerController
      } else {
          // Show error message
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-04
        • 2014-03-23
        • 2017-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-31
        相关资源
        最近更新 更多