Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 700
user2636326 Avatar asked Sep 07 '13 16:09

user2636326


2 Answers

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!

like image 52
Kyle Jurick Avatar answered Nov 20 '22 22:11

Kyle Jurick


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 .

like image 24
abhimuralidharan Avatar answered Nov 20 '22 23:11

abhimuralidharan