UIImagePickerController Showing Black Preview Screen

后端 未结 8 2253
醉梦人生
醉梦人生 2021-02-08 12:03

I am having a problem when calling the UIImagePickerController to use the camera. Sometimes, but more often than none, the preview screen shows to be black (as the camera itsel

8条回答
  •  情深已故
    2021-02-08 12:29

    I just fought this issue for a day and a half, sure enough I was doing something UI related outside the main thread. In my case I was updating a label in the response handling code for an asynchronous web service call.

    In an app with several view controllers that have 2000+ lines of code each in them, this kind of thing can be very difficult to track down. This is what finally brought me to my answer, and VERY quickly at that.

    https://gist.github.com/steipete/5664345

    This guy posted a class you can download and add to your project. Simply add it to your project, you don't need to use it anywhere or try to instantiate anywhere, just add it to your project. He also states that you should run this without ARC. To do that, go to your Build Phases tab, expand Compile Sources, double click on the file and then type -fno-objc-arc

    Now, run your project and navigate to where you are experiencing the black image preview screen. If all goes to plan, at some point prior to that your app should crash and dump to console some information about performing a UIKit action outside the main thread. Based on what your app was doing and what was dumped to console, you should be able to very quickly find the line of code causing you troubles. In my case I was able to call my response handler with a

    [self performSelectorOnMainThread:@selector(handleResponse:) withObject:data waitUntilDone:true];
    

    and my problem was gone immediately

    Also, this issue only began once I updated to iOS 7, I never saw this in iOS 5 or iOS 6.

    The guy who posted the file advises that you do not ship your app with this file included in your project and I strongly agree with that.

    Happy debugging!

提交回复
热议问题