10

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 itself is covered). After doing some research, it seems that people where not delegating it correctly..however, I believe my set up is correct. A restart of the app is what fixes it.

In my .h file I have included UIImagePickerControllerDelegate and UINavigationControllerDelegate.

Here is the code for the .m file

- (IBAction)camera:(id)sender {
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

    #if TARGET_IPHONE_SIMULATOR
        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    #else
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    #endif
        imagePickerController.editing = YES;
        imagePickerController.delegate = self;
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }

Any ideas as to why this is happening?

Thank you

user2636326
  • 113
  • 1
  • 7
  • 2
    Did you solve it finally? I'm having the same issue in ios 7 – titicaca Oct 01 '13 at 21:19
  • 4
    I'd be interested in a solution, too. Happened first in iOS7 - the black preview stays there for 30 seconds and works fine after that. – Boni2k Oct 17 '13 at 11:29
  • 1
    It really seems to happen when your app runs out of memory. Doesn't make it any easier to solve, though. – Boni2k Oct 20 '13 at 17:28
  • 2
    Hi Sorry to revive an old post, but I too have this issue, and you didn't post an answer. Post it if you found a solution. Thank you very much! – daivuk Oct 30 '13 at 19:28

8 Answers8

4

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!

Kyle Jurick
  • 244
  • 2
  • 15
  • This helped me! Thanks! In my case I was dismissing an earlier viewController from a thread other than main. Event though this misstep happened well before any subsequent efforts to open UIImagePickerController, putting it onto the main thread fixed my problem. – Andy Milburn Aug 27 '14 at 20:39
  • Great! Did you use the file above to find your issue, or did you take the manual approach of looking at all of your UI Kit calls? I found the file method to be super handy. It would be nice if the debugger threw an error about this instead of having these crazy side effects. – Kyle Jurick Aug 28 '14 at 17:38
  • I just ran into this problem, and contrary to many other guesses on StackOverflow, your answer got me looking at the correct cause. Thanks. – Mike Hay Sep 11 '14 at 01:32
  • Correct me, if i'm wrong. `Main Thread Checker in XCode11 should be able to do this now.` – Varun Parakh May 06 '20 at 07:22
4

Try this. it solved my problem, make sure that there is a value

(Application name as string) in your info.plist > "Bundle display name".

In my case it was empty and because of that it didn't work.

If "Bundle display name" is not there in the info.plist,then add a row named "Bundle display name" and paste your appname .

abhimuralidharan
  • 5,752
  • 5
  • 46
  • 70
0

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 .

Hooda
  • 1,157
  • 1
  • 9
  • 16
  • 2
    I am actually testing the code on the device when it is showing the black preview screen. Also, I am not using allowsImageEditing..any other thoughts? Thanks you. – user2636326 Sep 08 '13 at 04:45
0

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.

user564904
  • 216
  • 2
  • 6
0

Try this...

dispatch_async(dispatch_get_current_queue(), ^(void){
    imagePicker.sourceType =
    UIImagePickerControllerSourceTypeCamera;
    imagePicker.delegate = self;
    [self presentModalViewController:imagePicker animated:YES];
    } ) ;
0

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.

superandrew
  • 1,741
  • 19
  • 35
0

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.

-1

Please check [UIApplication sharedApplication].keyWindow.frame