From 62d5ce494c9162e33a4288d9535976dc8653b6a3 Mon Sep 17 00:00:00 2001 From: Peter Pozsonyi Date: Mon, 2 Oct 2023 13:43:43 +0300 Subject: [PATCH] iPhone 15 support fix --- ios/CameraView+Orientation.swift | 41 +++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/ios/CameraView+Orientation.swift b/ios/CameraView+Orientation.swift index e647b8e35d..850d8ff3e8 100644 --- a/ios/CameraView+Orientation.swift +++ b/ios/CameraView+Orientation.swift @@ -8,11 +8,39 @@ import Foundation import UIKit +import AVFoundation extension CameraView { + + private func interfaceOrientation(from: UIDeviceOrientation) -> UIInterfaceOrientation { + switch (from) { + case .landscapeLeft: + return .landscapeRight + case .landscapeRight: + return .landscapeLeft + case .portraitUpsideDown: + return .portraitUpsideDown + default: + return .portrait + } + } + + private func captureVideoOrientation(from: UIInterfaceOrientation) -> AVCaptureVideoOrientation { + switch (from) { + case .landscapeLeft: + return .landscapeLeft + case .landscapeRight: + return .landscapeRight + case .portraitUpsideDown: + return .portraitUpsideDown + default: + return .portrait + } + } + /// Orientation of the input connection (preview) private var inputOrientation: UIInterfaceOrientation { - return .portrait + return self.interfaceOrientation(from: UIDevice.current.orientation) } // Orientation of the output connections (photo, video, frame processor) @@ -41,5 +69,16 @@ extension CameraView { connection.setInterfaceOrientation(connectionOrientation) } } + + // update the preview layer orientation when the user rotates the device + // adapted from https://stackoverflow.com/a/36575423 + DispatchQueue.main.async { + if let previewLayerConnection: AVCaptureConnection = self.videoPreviewLayer.connection { + if previewLayerConnection.isVideoOrientationSupported { + previewLayerConnection.videoOrientation = self.captureVideoOrientation(from: connectionOrientation) + self.videoPreviewLayer.frame = self.bounds + } + } + } } }