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

在这里输入图像说明 我正在开发一个应用程序,捕捉从iDevice的相机的图像,并将其上传到networking服务。

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

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

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

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

我有一个有这个问题的客户。 他们必须select不允许访问摄像机。 我们必须在设置中更改应用程序的相机隐私设置。 当我们切换回来,没有更多的黑色相机屏幕。

我在iOS7面临同样的问题大约一个月,经过漫长的头脑破坏整个应用程序的代码审查,我能够确定问题。 我列举了一个
IBOutletCollection(UILabel)NSArray * staticLabelsCollection; 数组同时更新在多个线程上同时执行的标签文本。

 [self.labelsArr enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger idx, BOOL *stop) { UILabel * label = (UILabel*)obj; label.text=[NSString stringWithFormat:@"%d",idx+2]; }]; 

这造成了更新主线程以外的UIKit元素的问题。 我能够通过在Xcode中启用环境variablesCA_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 

通过强制这些“uncommited CAT交易”在主线程上运行,解决了黑色相机问题。 我能够解决它通过从枚举中删除选项:NSEnumerationConcurrent。

可以不断重现问题的示例应用程序可以在这里下载

希望示例应用程序可以提供一些见解和解决问题的工作。

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