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

Posted

技术标签:

【中文标题】切换到视频捕获模式后如何修复 UIImagePickerController 崩溃?【英文标题】:How to fix UIImagePickerController crash after switching to video capture mode? 【发布时间】:2019-09-10 11:30:55 【问题描述】:

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

步骤:

    呈现 UIImagePickerController 关闭 UIImagePickerController 呈现 UIImagePickerController 点击视频按钮 -> 崩溃

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

【问题讨论】:

您可能发现了一个不错的错误。您能否通过包含以下行 NSLog(@"Can set medium session preset: %@", [[AVCaptureSession new] canSetSessionPreset: AVCaptureSessionPresetMedium] ? @"true" : @"false"); 并导入 AVFoundation 来检查捕获会话中有关支持相应预设的内容。它是每次都报告为支持还是第二次变为“假”? Matic Oblak,AVCaptureSessionPresetMedium 报告两次都支持。 【参考方案1】:

我做了一个替代不支持的颜色空间的解决方法。

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)
    

【讨论】:

以上是关于切换到视频捕获模式后如何修复 UIImagePickerController 崩溃?的主要内容,如果未能解决你的问题,请参考以下文章

从暗模式快速切换到亮模式时如何修复搜索栏

部署到 Ubuntu 18.04 服务器后如何在 Laravel 中修复“未捕获的 ReflectionException:类视图不存在”

切换到 64 位 Excel 后如何修复 VBA“类型不匹配”错误

UIImagePickerController 选择视频后卡在 iOS10 模拟器上压缩视频

修复滚动到某个部分后的背景图像更改

Android上捕获的视频不会旋转到纵向