iDevice相机显示黑色而不是预览

时间:2013-05-25 05:48:18

标签: iphone ios ipad camera

enter image description here我正在开发一个应用程序,可以从iDevice的相机中捕获图像并将其上传到Web服务。

没问题除了设备的相机外,一切正常。设备的相机让我疯狂。我使用下面的代码来允许用户捕获图像。有时相机显示预览,有时不显示。而不是预览只是在屏幕上显示完全黑暗。如果我从后面切换到前置摄像头,则开始正常工作。我甚至尝试从设备中删除所有后台应用程序并清除尽可能多的内存;仍然没有运气,我被卡住了。 :(

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

4 个答案:

答案 0 :(得分:7)

我有一位客户遇到过这个问题。他们必须选择不允许访问相机。我们必须在“设置”中更改应用程序的相机隐私设置。当我们重新打开时,不再有黑色相机屏幕。

答案 1 :(得分:2)

我在iOS7中遇到了同样的问题大约一个月,在对整个应用程序进行了长时间的突破性代码审查后,我能够识别问题。 我在列举一个 IBOutletCollection(UILabel)NSArray * staticLabelsCollection; array同时更新标签文本,这些文本在多个线程上同时执行。

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

    UILabel * label = (UILabel*)obj;

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

}];

这就产生了在主线程以外更新UIKit元素的问题。 我能够通过在Xcode中启用环境变量CA_DEBUG_TRANSACTIONS = 1来捕获此问题,该变量在设备控制台中生成警告

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

修复这些未经通讯的CATransactions&#39;通过强制它们在主线程上运行固定黑色相机问题。 我能够通过从枚举中删除Option:NSEnumerationConcurrent来修复它。

可以下载可以不断重现问题的示例应用here

希望示例应用可以为问题提供一些见解和解决方法。

答案 2 :(得分:0)

我在我的应用中遇到过这个问题。虽然我从来没有发现问题是什么,但我重写了我的代码以定义UIIMagePickerController类型的属性并在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:^{}];
  }
}

出于某种原因,这解决了预览有时显示黑屏的问题

答案 3 :(得分:-1)

在ios7中,你应该设置mainWindow.rootViewController =一个类的类是UIViewController。这对我有用。 如果rootViewController是其他的,例如:UITabbarController,UINavigationController ...,将出现相机的黑屏。

相关问题