App freezes when trying to imagine rear camera on iPad

The application I'm working on works on both the iPhone and iPad. One of the functionality of the application is to capture images from the camera. I am using UIImagePickerController for this function. Here is my code block,

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

The application works the way it was developed on the iPhone, when ever I run the same code on the iPad, the application freezes. This problem occurs only on the iPad, but only on the rear camera. If I select the front camera from the image picker, the application starts the camera, but it freezes when I press the camera switch button.

As far as I understand from the magazine, the problem occurs when the application tries to draw a camera.

Logs:

 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 I use for testing: iPad Air 2 - iOS 10.2

EDIT

The UIImagePickerController.isSourceTypeAvailable (.camera) code returns true for my test case. I have already added this control, but have not mentioned it before.

I also noticed that when the application is frozen, the memory consumption starts to increase, and in one place the application crashed due to using too much memory.

In addition, I created an empty project and implemented the same approach for capturing an image that worked the way it was created in an empty project. At the moment, I think this problem may be related to some project settings.

EDIT - 2

I added a symbolic breakpoint for CGPostError, here is stacktrace:

enter image description here

It appears that the reason for the failure is the invalid context sent to the UIProgressView.

Any help would be appreciated.

EDIT - 3

As I mentioned earlier, I check the UIImagePickerController.isSourceTypeAvailable (.camera) in my code block, the problem is not related to camera accessibility. Also, the camera works like a popover when I present it modally.

+6
source share
4 answers

Finally, I solved the problem,

I implement a thematic approach for all applications. In my AppDelegate file, I set user interface options during application initialization.

Here is the problem

There is a UISlider in the camera’s user interface, this slider is always displayed on the back of the iPad, but on the iPhone it is displayed only when using zoom.

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

These two lines change the appearance for all sliders in the application, which means that it also changes the user interface for the camera slider.

As you can see in the screenshot I provided, when drawing the CoreGraphics library, the camera calls

  [UISlider setMinimumTrackTintColor]; 

Somehow setting the minimumTrackTintColor for the slider causes an invalid context error.

In addition, the thumTintColor setting works fine, it also changes the thumb color for the UISlider in Camera :)

Perhaps this problem is related to Swift 3, I will report an error and we will see :)

+2
source

You should check if the camera is available or not:

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

Here you can set the environment variable: fooobar.com/questions/21843 / ...

And here is the code that I use to open the camera in a universal application:

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

Replace the code as follows:

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) } 
+1
source

Source: https://habr.com/ru/post/1013377/


All Articles