iDevice camera shows black instead of preview

2019-01-14 08:42发布

问题:

I 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];        
    }    
}

回答1:

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



回答2:

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.



回答3:

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.



回答4:

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.