UIImagePickerController with UIImagePickerControllerSourceTypeCamera showing a black screen

I have a random problem that occurs when I present a UIImagePickerController to capture a photo. Sometimes the preview screen is just black. (it may be the first time, it may be the second time, or it may always or even never). However, I can see camera controls and yellow squares to detect faces. If I wait a while (about 20 seconds), the black preview disappears and the preview appears ...

Here is what I can sometimes see in magazines:

A snapshot of a snapshot that was not displayed results in an empty snapshot. Make sure your view has been viewed at least once before the snapshot or snapshot after screen updates.

config: xcode 5.1.1, iPhone 5 / 5S, ios 7.0 / 7.1

Here is the code that reproduces the problem for me:

 -(void) showCamera { UIImagePickerController *pickerController = [[UIImagePickerController alloc] init]; pickerController.allowsEditing = NO; pickerController.sourceType = UIImagePickerControllerSourceTypeCamera; pickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront; pickerController.cameraOverlayView = nil; UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController; while (topController.presentedViewController) { topController = topController.presentedViewController; } [topController presentViewController:pickerController animated:YES completion:nil]; } 

Of course, for my real application, I am testing if there is a camera with:

 [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]); 

I found several similar questions, but without real answers:

I tried what they offer, without success.

Edit: here is another code: As said, I made a singleton class, subclassing UIImagePickerController:

 @implementation BBImagePickerController static BBImagePickerController *sharedInstance = nil; + (BBImagePickerController *) sharedInstance { if (!sharedInstance) sharedInstance = [[BBImagePickerController alloc] init]; return sharedInstance; } @end 

Then I call it from the action sheet completion block using UIActionSheet + BlocksKit:

 UIActionSheet *actionSheetExt = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:cancel destructiveButtonTitle:nil otherButtonTitles:pickPic, takePic, nil]; [actionSheetExt showInView:presentingViewController.view]; [actionSheetExt bk_setDidDismissBlock:^(UIActionSheet *actionSheet, NSInteger click) { if (click == actionSheet.cancelButtonIndex) { NSLog(@"the user cancelled the action"); return ; } BBImagePickerController *pickerController = [BBImagePickerController sharedInstance]; pickerController.allowsEditing = YES; pickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; pickerController.delegate = self; if (click == 1) { // get from library if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; } else if (click == 2) { // take a picture if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { pickerController.sourceType = UIImagePickerControllerSourceTypeCamera; pickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront; //[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide]; } } [presentingViewController presentViewController:pickerController animated:YES completion:^{ NSLog(@"BBImagePickerController presented"); // Black preview screen }]; }]; 
+6
source share

Source: https://habr.com/ru/post/969907/


All Articles