iOS相机崩溃

3

我有一个与UIViewController连接的NavigationController,我称之为ScanViewController。在该控制器中,我想要启动相机,并且我只在viewDidLoad中有这个简单的代码片段,除此之外没有其他代码:

    let vc = UIImagePickerController()
    vc.sourceType = .camera
    vc.allowsEditing = true
    present(vc, animated: true)

但是在相机启动后立刻关闭并退出应用程序。输出如下。我有一个方向警告,但这不应该关闭相机,对吗?我也已经获得了相机权限,并通过在手机设置中进行检查来验证我的应用程序已启用相机访问权限。

代码中的权限:

    switch AVCaptureDevice.authorizationStatus(for: .video)
    case .authorized:
        print("Scan View Controller || Permission to use camera granted")
        self.startCamera()

在 info.plist 中的权限:

<key>NSCameraUsageDescription</key>
<string>$(PRODUCT_NAME) needs access to your camera.</string>

还有日志:

2023-03-17 11:28:12.627212+0100 Cykelrum.se[41417:2892989] [Camera] Attempted to change to mode Portrait with an unsupported device (BackWideDual). Auto device for both positions unsupported, returning Auto device for same position anyway (BackAuto).
2023-03-17 11:28:12.627307+0100 Cykelrum.se[41417:2892989] [Camera] Attempted to change to mode Portrait with an unsupported device (BackWideDual). Auto device for both positions unsupported, returning Auto device for same position anyway (BackAuto).
2023-03-17 11:28:12.627614+0100 Cykelrum.se[41417:2892989] [Camera] Attempted to change to mode Portrait with an unsupported device (BackAuto). Auto device for both positions unsupported, returning Auto device for same position anyway (BackAuto).
2023-03-17 11:28:12.627641+0100 Cykelrum.se[41417:2892989] [Camera] Attempted to change to mode Portrait with an unsupported device (BackWideDual). Auto device for both positions unsupported, returning Auto device for same position anyway (BackAuto).
2023-03-17 11:28:13.608818+0100 Cykelrum.se[41417:2893505] XPC connection interrupted
2023-03-17 11:28:13.611892+0100 Cykelrum.se[41417:2893494] [Common] [SBSSystemServiceClient:0x282a8d080] Service suspended: the connection with the service host has been interrupted.
2023-03-17 11:28:13.612078+0100 Cykelrum.se[41417:2893494] [Common] [FBSOrientationObserverClient:0x282abbea0] Service suspended: the connection with the service host has been interrupted.
2023-03-17 11:28:14.352022+0100 Cykelrum.se[41417:2893505] [Common] [FBSOrientationObserverClient:0x282abbea0] Service suspended: the connection with the service host has been interrupted.
2023-03-17 11:28:14.352115+0100 Cykelrum.se[41417:2893505] [Common] [SBSSystemServiceClient:0x282a8d080] Service suspended: the connection with the service host has been interrupted.
2023-03-17 11:28:22.302388+0100 Cykelrum.se[41417:2893494] 10.6.0 - [FirebaseAnalytics][I-ACS800014] Cannot get flag for unregistered flag. SDK name, flag name: app_measurement, session_stitching_token_feature_enabled
2023-03-17 11:28:23.630021+0100 Cykelrum.se[41417:2893414] [connection] nw_connection_copy_connected_local_endpoint_block_invoke [C27] Client called nw_connection_copy_connected_local_endpoint on unconnected nw_connection
2023-03-17 11:28:23.630078+0100 Cykelrum.se[41417:2893414] [connection] nw_connection_copy_connected_remote_endpoint_block_invoke [C27] Client called nw_connection_copy_connected_remote_endpoint on unconnected nw_connection
2023-03-17 11:28:23.630117+0100 Cykelrum.se[41417:2893414] [connection] nw_connection_copy_protocol_metadata_internal_block_invoke [C27] Client called nw_connection_copy_protocol_metadata_internal on unconnected nw_connection
2023-03-17 11:28:24.463510+0100 Cykelrum.se[41417:2893414] [connection] nw_resolver_start_query_timer_block_invoke [C25.1.1] Query fired: did not receive all answers in time for app-measurement.com:443

你在info.plist和代码中都有足够的相机权限吗? - Lawrence Gimenez
另外,我注意到您没有实现UIImagePickerController的委托调用。 - Lawrence Gimenez
@LawrenceGimenez 相机启动后立即变黑并关闭相机和应用程序。 - Yupi
1个回答

0

我曾经遇到过这种错误,当时我的设备相机出现了故障。您可能需要修改代码以检查可用的相机模式并仅使用支持的模式。以下是您可以修改代码的示例:

if UIImagePickerController.isSourceTypeAvailable(.camera) {
    imagePickerController.sourceType = .camera

    // Check available camera modes
    if let availableCameraModes = UIImagePickerController.availableCaptureModes(for: .rear) {
        // Only use supported camera modes
        if availableCameraModes.contains(.video) {
            imagePickerController.cameraCaptureMode = .video
        } else {
            // Handle unsupported camera mode error
            print("Error: Photo capture mode not available")
            return
        }
    }

    // Present image picker controller
    present(imagePickerController, animated: true, completion: nil)
} else {
    // Handle unavailable camera error
    print("Error: Camera not available")
}

谢谢你的回答。我会尝试一下并告诉你结果。再次感谢。 - Yupi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接