iOS 10 UIImagePickerController黑屏

iOS 10 UIImagePickerController black screen

我遇到了一个奇怪的问题....
在设备上 运行 iOS 10 相机预览显示黑屏。
但在 旧版本中它工作正常
我正在使用 UIImagePickerController

self.sourceType = UIImagePickerControllerSourceTypeCamera;

我的 info.plist 文件:

示例项目:https://github.com/arturdev/cameraTest

如果您使用 Xcode 8

,您需要将相机的隐私设置添加到 info.plist 才能访问相机

可能的原因可能是以下列出的原因之一,请尝试检查是否有帮助:

------ 在主线程中尝试与显示 UIImagePickerController 相关的代码:

dispatch_async(dispatch_get_current_queue(), ^(void){

    //Code related to show UIImagePickerController

    .....

    self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    self.imagePickerController.showsCameraControls = NO;
    self.imagePickerController.delegate = self;

    [self.view addSubview:self.imagePickerController.view];
    [self addChildViewController:self.imagePickerController];
    [self.imagePickerController didMoveToParentViewController:self];

    .....

});

----- 检查 info.plist >Bundle display name:

如果 info.plist >Bundle display name 为空,则添加 Bundle display name 值为 的键您的应用名称.

------ 将相机的隐私设置设置为 info.plist 以访问相机:

相机:

密钥:Privacy - Camera Usage Description
值:$(PRODUCT_NAME) camera use

------ 不要继承 UIImagePickerController:(如 Ankit Gupta 的 回答建议)

The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing.

将自定义视图分配给 cameraOverlayView 属性 并使用该视图来显示其他信息或管理相机界面与您的代码之间的交互。

参考:https://developer.apple.com/reference/uikit/uiimagepickercontroller

可能是您忘记显示 UIImagePickerController,这就是您出现黑屏的原因:-

[self presentViewController:yourImagePickerController animated:YES completion:NULL];

我遇到了同样的问题。 iOS10 有点奇怪。所以在这里我想出了解决方案。即使您正在使用 UIImagePickerController 或使用 viewController 扩展它。只需创建一个 UIImagePickerController 实例并将其呈现在 viewDidLoad 中。

UIImagePickerController *imagePickerController = [UIImagePickerController alloc];

[self presentViewController:imagePickerController animated:YES completion:nil];

我用不同的方法解决了这个问题。

不要创建 UIImagePickerController 的子类,因为 Apple 不支持 UIImagePickerController 的子类。

重要

The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified, with one exception. You can assign a custom view to the cameraOverlayView property and use that view to present additional information or manage the interactions between the camera interface and your code.

这是替代解决方案:

在 UIViewController 中添加这段代码

self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePickerController.showsCameraControls = NO;
self.imagePickerController.delegate = self;

[self.view addSubview:self.imagePickerController.view];
[self addChildViewController:self.imagePickerController];
[self.imagePickerController didMoveToParentViewController:self];

在所有 iOS 版本和设备上工作正常。 :):)

您是否使用 UIActionSheet 来显示 UIImagePickerController?我通过用 UIAlertController 替换 UIActionSheet 来解决它。因为 UIAlertView 可能会导致意外 bugs.DO 不要再使用 UIAlertView;)

使用 UIImagePickerController() 而不是任何其他初始化程序。

我遇到了同样的问题,是调用了错误的初始化器造成的。