user2636326
user2636326

Reputation: 113

UIImagePickerController Showing Black Preview Screen

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

Upvotes: 10

Views: 6021

Answers (8)

German Battiston
German Battiston

Reputation: 1

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.

Upvotes: 0

superandrew
superandrew

Reputation: 1781

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.

Upvotes: 0

Wang Juanyong
Wang Juanyong

Reputation: 1

Please check [UIApplication sharedApplication].keyWindow.frame

Upvotes: -1

abhimuralidharan
abhimuralidharan

Reputation: 5939

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 .

Upvotes: 4

user3522125
user3522125

Reputation: 1

Try this...

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

Upvotes: 0

Kyle Jurick
Kyle Jurick

Reputation: 242

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!

Upvotes: 4

user564904
user564904

Reputation: 216

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.

Upvotes: 0

Hooda
Hooda

Reputation: 1187

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 .

Upvotes: 0

Related Questions