尝试在iPad上显示后置摄像头时,应用程序会冻结

我正在使用的应用程序在iPhone和iPad上运行。 应用程序的function之一就是从相机捕捉图像。 我正在使用UIImagePickerController这个function。 这是我的代码块;

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

该应用程序工作,因为它在iPhonedevise的,当我在iPad上运行相同的代码的应用程序冻结。 这个问题只发生在iPad,但只为后置摄像头。 如果我从图像select器中select前置摄像头,则应用程序会启动摄像头,但是当按下切换摄像头button时会冻结。

据我所知日志问题发生时,应用程序正在试图画相机。

日志:

 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. 

我用来testing的iPad是:iPad Air 2 – iOS 10.2

编辑

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

另外我注意到,当应用程序被冻结时,内存消耗开始增加,并在同一点应用程序因使用太多的内存而崩溃。

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

编辑 – 2

我添加了CGPostError的符号断点,这里是stacktrace:

在这里输入图像说明

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

任何帮助将不胜感激。

编辑 – 3

正如我前面提到的,我检查我的代码块中的UIImagePickerController.isSourceTypeAvailable(.camera) ,问题与​​相机可用性无关。 当我以模态方式呈现时,相机也可以作为popup窗口使用。

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

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

最后我解决了这个问题,

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

这是问题,

在相机用户界面中有一个UISlider,这个滑块视图总是在iPad的后置摄像头中可见,但在iPhone中,只有在使用变焦时才能看到。

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

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

正如你所看到的,我在屏幕截图上看到了CoreGraphics库的相机调用

  [UISlider setMinimumTrackTintColor]; 

以某种方式设置滑块的minimumTrackTintColor导致无效的上下文错误。

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

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

以下是如何设置环境variables: 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) } 

用这个代替你的代码:

Swift 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) } 

Swift 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) }