From a6e828adaaa1c9caea55fbae77a2f340bfe05311 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Wed, 10 Jul 2024 12:55:19 +0200 Subject: [PATCH] fix: Make `AVCaptureDevice.sensorOrientation` safer --- .../Extensions/AVCaptureDevice+sensorOrientation.swift | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/package/ios/Core/Extensions/AVCaptureDevice+sensorOrientation.swift b/package/ios/Core/Extensions/AVCaptureDevice+sensorOrientation.swift index 92d191b0dd..7c259095ea 100644 --- a/package/ios/Core/Extensions/AVCaptureDevice+sensorOrientation.swift +++ b/package/ios/Core/Extensions/AVCaptureDevice+sensorOrientation.swift @@ -23,7 +23,9 @@ extension AVCaptureDevice { // 1. Create a capture session let session = AVCaptureSession() - session.sessionPreset = .low + if session.canSetSessionPreset(.low) { + session.sessionPreset = .low + } // 2. Add this device as an input guard let input = try? AVCaptureDeviceInput(device: self) else { @@ -31,12 +33,18 @@ extension AVCaptureDevice { "falling back to \(DEFAULT_SENSOR_ORIENTATION)...") return DEFAULT_SENSOR_ORIENTATION } + guard session.canAddInput(input) else { + return DEFAULT_SENSOR_ORIENTATION + } session.addInput(input) // 3. Add an output (e.g. video data output) let output = AVCaptureVideoDataOutput() output.automaticallyConfiguresOutputBufferDimensions = false output.deliversPreviewSizedOutputBuffers = true + guard session.canAddOutput(output) else { + return DEFAULT_SENSOR_ORIENTATION + } session.addOutput(output) // 4. Inspect the default orientation of the output