切换到视频捕获模式后如何修复UIImagePicker控制器崩溃?



切换到视频捕获模式后,我在iPhone XR(iOS 12.4.1(真实设备上使用UIImagePickerController崩溃。崩溃重现在UIImagePickerController的第二次演示中。 崩溃不会在iPhone 6(iOS 12.4.1(真实设备上重现。 摄像头和麦克风用法说明键在 Info.plist 中设置 使用 Xcode 10.3 构建的测试应用

步骤:

  1. 当前 UIImagePickerController
  2. 解除 UIImagePickerController
  3. 当前 UIImagePickerController
  4. 点击视频按钮 ->崩溃

如果我设置videoQuality = UIImagePickerControllerQualityTypeHigh崩溃,崩溃不会重现。但我需要UIImagePickerControllerQualityTypeMedium.

@interface ViewController () < UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@end
@implementation ViewController
#pragma mark - IBActions
- (IBAction)showPicker:(UIButton *)sender {
UIImagePickerController *pickerController = [UIImagePickerController new];
pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
pickerController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
//    pickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
pickerController.delegate = self;
[self presentViewController:pickerController animated:YES completion:NULL];
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info {
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:NULL];
}
@end
'NSInvalidArgumentException', reason: '*** -[AVCaptureDevice setActiveColorSpace:] Not supported - use activeFormat.supportedColorSpaces'

我做了一个解决方法来替换不受支持的色彩空间。

private extension AVCaptureDevice {

static let configureRandomCrashWorkaround: Void = {
swizzleInstanceMethod(
class: AVCaptureDevice.self,
originalSelector: #selector(setter: AVCaptureDevice.activeColorSpace),
swizzledSelector: #selector(AVCaptureDevice.kjy_swizzle_setActiveColorSpace)
)
}()

@objc func kjy_swizzle_setActiveColorSpace(_ colorSpace: AVCaptureColorSpace) {
var colorSpace = colorSpace
let supportedColorSpaces = activeFormat.supportedColorSpaces

if !supportedColorSpaces.isEmpty,
!supportedColorSpaces.contains(colorSpace)
{
// prevent a crash on UIImagePickerControllerInfoKey/Camera: "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: -[AVCaptureDevice setActiveColorSpace:] Not supported - use activeFormat.supportedColorSpaces"
colorSpace = activeFormat.supportedColorSpaces[0]
}
kjy_swizzle_setActiveColorSpace(colorSpace)
}
}

相关内容

  • 没有找到相关文章

最新更新