From a037beaabf7013b95ae1a125cdf656079fdb626f Mon Sep 17 00:00:00 2001 From: Pushpender Singh Date: Tue, 25 Nov 2025 14:36:40 +0530 Subject: [PATCH 1/4] fix: update video orientation handling in CameraView for session readiness --- ios/CameraView.swift | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/ios/CameraView.swift b/ios/CameraView.swift index 23f6b80..22c6661 100644 --- a/ios/CameraView.swift +++ b/ios/CameraView.swift @@ -48,7 +48,7 @@ public class CameraView: UIView { guard let self = self else { return } if let previewLayer = self.previewLayer { previewLayer.session = session - previewLayer.connection?.videoOrientation = .portrait + self.updateVideoOrientation() print("✅ Preview layer bound to session from onSessionReady callback") } else { print("⚠️ Preview layer missing when session became ready") @@ -62,7 +62,7 @@ public class CameraView: UIView { guard let self = self, let previewLayer = self.previewLayer else { return } if let existingSession = existingSession { previewLayer.session = existingSession - previewLayer.connection?.videoOrientation = .portrait + self.updateVideoOrientation() print("✅ Preview layer bound to existing session") } } @@ -73,6 +73,26 @@ public class CameraView: UIView { public override func layoutSubviews() { super.layoutSubviews() previewLayer?.frame = bounds + updateVideoOrientation() + } + + private func updateVideoOrientation() { + guard let connection = previewLayer?.connection, connection.isVideoOrientationSupported else { return } + + let orientation = UIDevice.current.orientation + + switch orientation { + case .portrait: + connection.videoOrientation = .portrait + case .landscapeRight: + connection.videoOrientation = .landscapeLeft + case .landscapeLeft: + connection.videoOrientation = .landscapeRight + case .portraitUpsideDown: + connection.videoOrientation = .portraitUpsideDown + default: + break + } } deinit { From 30130023ec9443a111e8cc15a863edefb7c53626 Mon Sep 17 00:00:00 2001 From: Pushpender Singh Date: Tue, 25 Nov 2025 14:46:36 +0530 Subject: [PATCH 2/4] fix: improve video orientation handling in CameraView to support interface orientation fallback --- ios/CameraView.swift | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/ios/CameraView.swift b/ios/CameraView.swift index 22c6661..49a0386 100644 --- a/ios/CameraView.swift +++ b/ios/CameraView.swift @@ -79,20 +79,41 @@ public class CameraView: UIView { private func updateVideoOrientation() { guard let connection = previewLayer?.connection, connection.isVideoOrientationSupported else { return } - let orientation = UIDevice.current.orientation + let deviceOrientation = UIDevice.current.orientation - switch orientation { + let videoOrientation: AVCaptureVideoOrientation + + switch deviceOrientation { case .portrait: - connection.videoOrientation = .portrait + videoOrientation = .portrait case .landscapeRight: - connection.videoOrientation = .landscapeLeft + videoOrientation = .landscapeLeft case .landscapeLeft: - connection.videoOrientation = .landscapeRight + videoOrientation = .landscapeRight case .portraitUpsideDown: - connection.videoOrientation = .portraitUpsideDown + videoOrientation = .portraitUpsideDown default: - break + // For .faceUp, .faceDown, .unknown, or during transitions, + // fall back to the interface orientation from the window scene + if let windowScene = window?.windowScene { + switch windowScene.interfaceOrientation { + case .portrait: + videoOrientation = .portrait + case .landscapeRight: + videoOrientation = .landscapeRight + case .landscapeLeft: + videoOrientation = .landscapeLeft + case .portraitUpsideDown: + videoOrientation = .portraitUpsideDown + default: + videoOrientation = .portrait + } + } else { + videoOrientation = .portrait + } } + + connection.videoOrientation = videoOrientation } deinit { From f7b3bad9feb521956912cff47c60058bf5c85a3d Mon Sep 17 00:00:00 2001 From: Pushpender Singh Date: Tue, 25 Nov 2025 14:56:52 +0530 Subject: [PATCH 3/4] fix: add orientation change handling in CameraView and manage observer lifecycle --- ios/CameraView.swift | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/ios/CameraView.swift b/ios/CameraView.swift index 49a0386..5a94cd7 100644 --- a/ios/CameraView.swift +++ b/ios/CameraView.swift @@ -13,15 +13,18 @@ public class CameraView: UIView { private var previewLayer: AVCaptureVideoPreviewLayer? private var cameraManager: CameraManager? + private var currentVideoOrientation: AVCaptureVideoOrientation = .portrait public override init(frame: CGRect) { super.init(frame: frame) setupView() + setupOrientationObserver() } required init?(coder: NSCoder) { super.init(coder: coder) setupView() + setupOrientationObserver() } private func setupView() { @@ -36,6 +39,19 @@ public class CameraView: UIView { } } + private func setupOrientationObserver() { + NotificationCenter.default.addObserver( + self, + selector: #selector(handleOrientationChange), + name: UIDevice.orientationDidChangeNotification, + object: nil + ) + } + + @objc private func handleOrientationChange() { + updateVideoOrientation() + } + @objc public func setCameraManager(_ manager: CameraManager) { self.cameraManager = manager @@ -73,7 +89,6 @@ public class CameraView: UIView { public override func layoutSubviews() { super.layoutSubviews() previewLayer?.frame = bounds - updateVideoOrientation() } private func updateVideoOrientation() { @@ -113,10 +128,17 @@ public class CameraView: UIView { } } + // Only update if orientation actually changed + guard videoOrientation != currentVideoOrientation else { return } + + currentVideoOrientation = videoOrientation connection.videoOrientation = videoOrientation } deinit { + // Remove orientation observer + NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil) + // CHANGE: Clear session on teardown to avoid retaining references. previewLayer?.session = nil previewLayer?.removeFromSuperlayer() From b05142fe4f67205bc34de6c3d3fb9e9f18bda153 Mon Sep 17 00:00:00 2001 From: Pushpender Singh Date: Tue, 25 Nov 2025 15:12:50 +0530 Subject: [PATCH 4/4] fix: manage device orientation notifications in CameraView setup and teardown --- ios/CameraView.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ios/CameraView.swift b/ios/CameraView.swift index 5a94cd7..7436762 100644 --- a/ios/CameraView.swift +++ b/ios/CameraView.swift @@ -40,6 +40,9 @@ public class CameraView: UIView { } private func setupOrientationObserver() { + // Enable device orientation notifications + UIDevice.current.beginGeneratingDeviceOrientationNotifications() + NotificationCenter.default.addObserver( self, selector: #selector(handleOrientationChange), @@ -136,8 +139,9 @@ public class CameraView: UIView { } deinit { - // Remove orientation observer + // Remove orientation observer and stop generating notifications NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil) + UIDevice.current.endGeneratingDeviceOrientationNotifications() // CHANGE: Clear session on teardown to avoid retaining references. previewLayer?.session = nil