iOS 相机崩溃

iOS Camera crash

提问人:Yupi 提问时间:3/17/2023 最后编辑:Steve MarooniYupi 更新时间:3/21/2023 访问量:417

问:

我有一个与我联系的。在那个控制器中,我想启动相机,我只有这个简单的代码片段,旁边没有代码:NavigationControllerUIViewControllerScanViewControllerviewDidLoad

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

但是在相机启动后,它会立即关闭并关闭应用程序。输出如下。我有一个方向警告,但这不应该关闭相机或?我有相机权限,我也通过签入手机和我的应用程序启用了相机访问权限来验证这一点。Settings

代码中的权限:

    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
ios swift ios相机

评论

0赞 Lawrence Gimenez 3/17/2023
您在 info.plist 和代码中都有足够的相机权限吗?
0赞 Lawrence Gimenez 3/17/2023
另外,我注意到您没有实现 UIImagePickerController 的委托调用。
0赞 Yupi 3/17/2023
@LawrenceGimenez,因此在相机启动后,它会立即变黑并关闭相机和应用程序。

答:

0赞 Sarvajeet Singh 3/17/2023 #1

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

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

评论

0赞 Yupi 3/17/2023
谢谢你的回答。我会试一试,让你知道。再次感谢。