iDevice camera shows black instead of preview
Asked Answered
A

4

12

enter image description hereI am developing an app that captures images from iDevice's camera and upload it to web service.

NO problem everything is working fine except the device's camera. Device's camera is driving my crazy. I am using below code to allow user to capture images. Sometimes camera shows preview and sometimes doesn't. Instead of preview is just shows complete darkness on screen. If I switch from rear to front camera is starts working fine. I have even trying deleting all background apps from device and clearing as much memory as I could; still no luck and I am stuck. :(

- (IBAction)addNewImage:(id)sender
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        // Take picture from camera
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

        // set no to take as much pictures as user want.
        imagePicker.showsCameraControls = YES;

        // Show user the camera
        [self presentModalViewController:imagePicker
                                animated:YES];
    }
    else
    {
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentModalViewController:imagePicker
                                animated:YES];        
    }    
}
Asyndeton answered 25/5, 2013 at 5:48 Comment(6)
Nothing stands out in your code as being incorrect (except I'd initialize your UIImagePickerController after you've confirmed isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera returns YES). Does it only happen on that one device? If so, perhaps something is malfunctioning in your camera. Have you tested it on other devices?Macario
Yes I have tested it on iPhone 4, iPad2 and iPad4. It gives same problem. Any clue please.Asyndeton
I have attached the screen shot of my view on camera.Asyndeton
Did you try creating a new single-view project with an only button that would call your addNewImage:? I tried this on my side and it worked just as expected.Canna
possible duplicate of iOS 7 UIImagePicker preview black screenWashout
Possible duplicate of iOS app camera access denied iOS 9.1 **[solved]**Jostle
U
0

I had faced this issue in my app. Though I never found out the what the issue was, I rewrote my code to define a property of UIIMagePickerController type and initialize it once in the getter. Used this property to initialize the camera view :

getter:

-(UIImagePickerController *) imagePicker{
    if(!_imagePicker){
       _imagePicker = [[UIImagePickerController alloc] init];
       _imagePicker.delegate = self;
       if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
           _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
       }
       else{
           _imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
       }  

   }
    return _imagePicker;
}


- (IBAction)addNewImage:(id)sender{
  if (self.imagePicker)
  {
      [self presentViewController:self.imagePicker animated:YES completion:^{}];
  }
}

For some reason this got rid of the issue with preview sometimes showing a black screen

Unpractical answered 26/5, 2013 at 6:13 Comment(10)
Thanks for the reply Kedar. I tried code snippet given by you, but this trick doesn't seem to work.Asyndeton
Camera shows preview but it takes time can say 2 - 4 seconds. Any clue?Asyndeton
Do you have some blocks running on one of the GCD queues while you launch the imagepickercontroller? Have seen that causing this issue too.Unpractical
Sorry, got no more definitive clues. I was getting the problem when there were too many blocks being executed in the CGD queue with priority DISPATCH_QUEUE_PRIORITY_DEFAULT. NSOperationQueue should be fine. Still you could check if there are any operations being executed when the controller is launched and if stopping the operations helps, but I think its not related.Unpractical
Thanks Kedar for the help. Let me try your suggestionsAsyndeton
why is this the accepted answer if the correct answer hasn't been reached yet?Cantus
This worked for me... simply holding on to a reference to the UIImagePickerController and presenting that the second time (instead of creating a new one) made the difference. Odd.Osmosis
@Cantus this is the accepted answer because it helped me understand the problem.Asyndeton
The answer to #19082201 addresses the root cause of this problem.Washout
@Nirav Have you got it fixed?Jostle
K
7

I have a customer who had this issue. They must have selected to not allow access to the camera. We had to change the camera privacy setting for the app in Settings. When we switched that back on, no more black camera screen.

Kare answered 13/2, 2015 at 22:6 Comment(5)
These funny customers. :) Thanks and finally figured out why it didn't work on my client's phone.Excisable
Yep it's a love hate relationship... We later put in code to detect when it's deactivated and put a warning on the login screen.Kare
In settings->privacy->camera-> I can't see my app listed in this section.What am I doing wrong?And my camera appears black.Jostle
#34199861Jostle
Go to Settings -> AppName(example: Youtube) -> Camera -> You can see Switch Button -> You have enable it.Jamieson
S
2

I was facing the same issue in iOS7 for around a month, After a long long head breaking code review of the entire app, i was able to identify the problem. I was enumerating an
IBOutletCollection(UILabel) NSArray *staticLabelsCollection; array Concurrently updating the labels texts, which got executed simultaneously on multiple threads.

[self.labelsArr enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    UILabel * label = (UILabel*)obj;

    label.text=[NSString stringWithFormat:@"%d",idx+2];

}];

This created the problem of updating the UIKit elements on other than main thread. I was able to catch the this issue by enabling the environment variable CA_DEBUG_TRANSACTIONS=1 in Xcode which generated warnings in device console

Nov 20 18:40:26 iPad2 CameraTest[1757] <Warning>: CoreAnimation: warning, deleted thread with uncommitted CATransaction; created by:
 0   QuartzCore                          0x32a553b3 <redacted> + 266
 1   QuartzCore                          0x32a55269 <redacted> + 224
 2   QuartzCore                          0x32a56871 <redacted> + 24
 3   QuartzCore                          0x32a56eed <redacted> + 40
 4   QuartzCore                          0x32a619ed <redacted> + 412
 5   QuartzCore                          0x32a6184b <redacted> + 46
 6   QuartzCore                          0x32a61819 <redacted> + 44
 7   UIKit                               0x32ddfe53 <redacted> + 86
 8   CameraTest                          0x000923b5 __35-[ViewController blockEnumeration:]_block_invoke + 184
 9   CoreFoundation                      0x305aa821 <redacted> + 92
 10  libdispatch.dylib                   0x3b3308eb <redacted> + 134
 11  libdispatch.dylib                   0x3b32fd71 <redacted> + 220
 12  libdispatch.dylib                   0x3b32ff59 <redacted> + 56
 13  libsystem_pthread.dylib             0x3b46adbf _pthread_wqthread + 298
 14  libsystem_pthread.dylib             0x3b46ac84 start_wqthread + 8

Fixing these 'uncommited CATransactions' by forcing them to run on the main thread fixed the black camera issues. I was able to fix it by removing Option: NSEnumerationConcurrent from enumeration.

The sample app which could constantly reproduce the problem can be downloaded here

Hope the sample app could give some insight and the work around for the issue.

Sigismundo answered 21/11, 2013 at 7:32 Comment(0)
U
0

I had faced this issue in my app. Though I never found out the what the issue was, I rewrote my code to define a property of UIIMagePickerController type and initialize it once in the getter. Used this property to initialize the camera view :

getter:

-(UIImagePickerController *) imagePicker{
    if(!_imagePicker){
       _imagePicker = [[UIImagePickerController alloc] init];
       _imagePicker.delegate = self;
       if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
           _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
       }
       else{
           _imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
       }  

   }
    return _imagePicker;
}


- (IBAction)addNewImage:(id)sender{
  if (self.imagePicker)
  {
      [self presentViewController:self.imagePicker animated:YES completion:^{}];
  }
}

For some reason this got rid of the issue with preview sometimes showing a black screen

Unpractical answered 26/5, 2013 at 6:13 Comment(10)
Thanks for the reply Kedar. I tried code snippet given by you, but this trick doesn't seem to work.Asyndeton
Camera shows preview but it takes time can say 2 - 4 seconds. Any clue?Asyndeton
Do you have some blocks running on one of the GCD queues while you launch the imagepickercontroller? Have seen that causing this issue too.Unpractical
Sorry, got no more definitive clues. I was getting the problem when there were too many blocks being executed in the CGD queue with priority DISPATCH_QUEUE_PRIORITY_DEFAULT. NSOperationQueue should be fine. Still you could check if there are any operations being executed when the controller is launched and if stopping the operations helps, but I think its not related.Unpractical
Thanks Kedar for the help. Let me try your suggestionsAsyndeton
why is this the accepted answer if the correct answer hasn't been reached yet?Cantus
This worked for me... simply holding on to a reference to the UIImagePickerController and presenting that the second time (instead of creating a new one) made the difference. Odd.Osmosis
@Cantus this is the accepted answer because it helped me understand the problem.Asyndeton
The answer to #19082201 addresses the root cause of this problem.Washout
@Nirav Have you got it fixed?Jostle
B
-1

In ios7, you should set mainWindow.rootViewController = a class has kind is UIViewController. It's work for me. If rootViewController is other, ex: UITabbarController, UINavigationController..., the black screen of camera will appear.

Breeks answered 15/11, 2013 at 6:35 Comment(1)
hi, have you tried this sample of apple developer? developer.apple.com/LIBRARY/IOS/samplecode/PhotoPicker/…. There is not display black preview screen. At first, I set a uitabbarcontroller is rootviewController in mainWindow. The black preview is sometime show. After that, i set uiviewcontroller is rootviewcontroller. No black preview appear.Breeks

© 2022 - 2024 — McMap. All rights reserved.