iOS 7 UIImagePicker预览黑屏

当我尝试从我的代码加载相机,相机预览是黑色的。 如果我等待10-20秒,它将显示真实的相机预览。 我发现了几个问题,其中一些build议在后台运行其他代码应该是原因。 但是,我没有任何代码在后台运行。 我应该如何解决这个问题?

这是我的代码,我运行相机

UIImagePickerController *photoPicker = [[UIImagePickerController alloc] init]; photoPicker.delegate = self; photoPicker.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:photoPicker animated:YES completion:NULL]; 

大约5个月前,我的团队在iOS7中发现了UIImageViewController的内存泄漏。 每个实例都会以指数forms减慢应用程序(即第一个alloc-init有1秒的延迟,第二个有2秒的延迟,第三个有5秒的延迟)。 最后,我们有30-60个延迟(类似于你正在经历的)。

我们通过inheritanceUIImagePickerController并使其成为一个Singleton来解决问题。 这样,它只被初始化一次。 现在我们的延迟是最小的,我们避免了泄漏。 如果子类不是一个选项,在你的viewController中尝试一个类属性,只是像这样懒加载它。

 -(UIImagePickerController *)imagePicker{ if(!_imagePicker){ _imagePicker = [[UIImagePickerController alloc]init]; _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; } return _imagePicker; } 

那么你可以稍后调用它:

 [self presentViewController:self.imagePicker animated:YES completion:nil]; 

如果有这个我自己 – 如果在主调度线上运行的东西会发生 – 你有没有机会调整图像大小?

它把预览放到主线程上,如果有东西在使用它,你会得到一个黑屏。 这是一个错误,解决方法是接pipe主线程或禁用照片select器,直到队列空闲

这应该适合你:

  - (void)cameraViewPickerController:(UIImagePickerController *)picker { [self startCameraControllerFromViewController: picker usingDelegate: self]; } - (BOOL) startCameraControllerFromViewController: (UIViewController*) controller usingDelegate: (id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>) delegate { if (([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera] == NO) || (delegate == nil) || (controller == nil)) return NO; UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init]; cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera; // Displays a control that allows the user to choose movie capture cameraUI.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, (NSString *) kUTTypeMovie,nil]; // Hides the controls for moving & scaling pictures, or for // trimming movies. To instead show the controls, use YES. cameraUI.allowsEditing = NO; cameraUI.delegate = delegate; [controller presentViewController:cameraUI animated:YES completion:nil]; return YES; }