UIImagePickerController Showing Black Preview Screen

后端 未结 8 2249
醉梦人生
醉梦人生 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:10

    While it could be an issue that depends on many factor, as shown from the successful solutions provided by other members, I think it's worth mentioning the fact that the user might have denied permissions for camera usage (Under Privacy->Camera->Your application) and thus the camera shows, but displays a black preview screen. It just happened to me, and it depended from a bug in the early development stage in which that question was never seen by the user because of overlapping alerts.

    0 讨论(0)
  • 2021-02-08 12:14

    This can happen when there is animation not started on the main thread going on in parallel to kicking off the imagePickerController

    If this is what you are running into you will likely see

    <Warning>: CoreAnimation: warning, deleted thread with uncommitted CATransaction; set CA_DEBUG_TRANSACTIONS=1 in environment to log backtraces.

    On your console log

    I ran into it first by kicking off an activity indicator from a different thread (bug in its own right I know). But more insidiously this hit me due to loading a nib file in a background thread (which calls [CATransaction setDisableActions:] ... who knew)

    Turning on CA_DEBUG_TRANSACTIONS in your scheme and then running in your simulator while tailing the system log (tail -f /var/log/system.log) is really the best way to find out the specific culprit.

    0 讨论(0)
  • 2021-02-08 12:21

    Test this in your device. I think you are testing it in simulator and allowsImageEditing property Deprecated in iOS 3.1 so dont use it .

    0 讨论(0)
  • 2021-02-08 12:23

    Please check [UIApplication sharedApplication].keyWindow.frame

    0 讨论(0)
  • 2021-02-08 12:27

    When you're creating the UIImagePickerController change the cameraOverlayView to nil. It worked like a charm for me.

    self.imagePickerController.cameraOverlayView = nil;
    

    And that should be it.

    0 讨论(0)
  • 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!

    0 讨论(0)
提交回复
热议问题