切换到视频捕获模式后如何解决UIImagePickerController崩溃?

时间:2019-09-10 11:30:55

标签: ios crash uiimagepickercontroller iphone-xr

切换到视频捕获模式后,我在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. 点击VIDEO按钮->崩溃

如果我设置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'

1 个答案:

答案 0 :(得分:0)

我做了一个替代不支持的色彩空间的变通方法。

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)
    }
}