当我尝试从我的代码加载相机时,相机预览为黑色.如果我等待10-20秒,它将显示真实的相机预览.我发现了几个问题,其中一些问题表明在后台运行其他代码应该是这个问题的原因.但是我没有在后台运行任何代码.我该怎么解决这个问题?
这是我运行相机的代码
UIImagePickerController *photoPicker = [[UIImagePickerController alloc] init];
photoPicker.delegate = self;
photoPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:photoPicker animated:YES completion:NULL];
Run Code Online (Sandbox Code Playgroud) 我有一个使用sourceType相机调用的UIImagePickerController,80%的时间我得到黑色预览.如果我等待,让我们说大约30秒,我会得到一个很好的预览,它会在大约50%的时间内好,然后它会再次破裂.
有问题的图像与此非常相似.iDevice相机显示黑色而不是预览
其他人暗示GCD可能会导致相机出现问题,并且在加载图像选择器时更新UI会破坏它.我为每个调用主线程的GCD块设置了一个锁.
这是旋转以模拟活动指示器的图像的示例.
-(void)animateLoadingImage {
if (isMainThreadBlocked) {
return;
}
self.radians += M_PI_4 / 2;
[UIView beginAnimations:@"progress rotation" context:nil];
[UIView setAnimationDuration:.1];
self.loadingImageView.transform = CGAffineTransformMakeRotation(self.radians);
[UIView commitAnimations];
}
Run Code Online (Sandbox Code Playgroud)
PS:快照未渲染的视图会导致空快照.确保在屏幕更新后快照或快照之前至少渲染了一次视图.
这会在我尝试打开拾取器控制器时显示ALWAYS,但即使相机正确显示预览,它也会显示.我不认为错误在这里,但这也让我很烦恼.
调用UIImagePickerController使用相机时遇到问题.有时,但往往没有,预览屏幕显示为黑色(因为相机本身被覆盖).在做了一些研究后,似乎没有正确委托它的人......但是,我相信我的设置是正确的.重新启动应用程序是修复它的原因.
在我的.h文件中,我包含了UIImagePickerControllerDelegate和UINavigationControllerDelegate.
这是.m文件的代码
- (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];
}
Run Code Online (Sandbox Code Playgroud)
关于为什么会发生这种情况的任何想法?
谢谢