How to fix UIImagePickerController crash after switching to video capture mode?

给你一囗甜甜゛ 提交于 2021-02-19 04:28:06

问题


I have a crash with UIImagePickerController on iPhone XR(iOS 12.4.1) real device after switching to video capture mode. Crash reproduces on the second presentation of UIImagePickerController. Crash does not reproduce on iPhone 6(iOS 12.4.1) real device. Camera and Microphone usage description keys are set in Info.plist Test app built with Xcode 10.3

Steps:

  1. Present UIImagePickerController
  2. Dismiss UIImagePickerController
  3. Present UIImagePickerController
  4. Tap VIDEO button - > Crash

Crash does not reproduce if I set videoQuality = UIImagePickerControllerQualityTypeHigh. But I need 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:


I made a workaround which replaces unsupported color spaces.

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


来源:https://stackoverflow.com/questions/57869899/how-to-fix-uiimagepickercontroller-crash-after-switching-to-video-capture-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!