Skip to content

Commit 13fb2f1

Browse files
committed
fix(ios): fix image orientation when device is rotated
1 parent 3a601de commit 13fb2f1

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

example-app/src/global.scss

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,13 @@ body.camera-running {
3333

3434
.camera-modal {
3535
visibility: visible;
36-
}
36+
}
37+
38+
ion-modal {
39+
--height: 100%;
40+
--width: 100%;
41+
42+
ion-header ion-toolbar:first-of-type {
43+
padding-top: env(safe-area-inset-top);
44+
}
45+
}

ios/Sources/CameraViewPlugin/CameraViewManager.swift

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,19 @@ internal let SUPPORTED_CAMERA_DEVICE_TYPES: [AVCaptureDevice.DeviceType] = [
151151
photoSettings.flashMode = .off
152152
}
153153

154+
// Ensure proper orientation
155+
if let photoConnection = avPhotoOutput.connection(with: .video),
156+
let previewConnection = videoPreviewLayer.connection {
157+
if photoConnection.isVideoOrientationSupported {
158+
photoConnection.videoOrientation = previewConnection.videoOrientation
159+
}
160+
161+
// Handle mirroring for front camera
162+
if photoConnection.isVideoMirroringSupported && cameraDevice.position == .front {
163+
photoConnection.isVideoMirrored = true
164+
}
165+
}
166+
154167
avPhotoOutput.capturePhoto(with: photoSettings, delegate: self)
155168
photoCaptureHandler = completion
156169
}
@@ -169,22 +182,25 @@ internal let SUPPORTED_CAMERA_DEVICE_TYPES: [AVCaptureDevice.DeviceType] = [
169182
return
170183
}
171184

185+
// Ensure proper orientation
186+
if let photoConnection = avPhotoOutput.connection(with: .video),
187+
let previewConnection = videoPreviewLayer.connection {
188+
if photoConnection.isVideoOrientationSupported {
189+
photoConnection.videoOrientation = previewConnection.videoOrientation
190+
}
191+
192+
// Handle mirroring for front camera
193+
if photoConnection.isVideoMirroringSupported && cameraDevice.position == .front {
194+
photoConnection.isVideoMirrored = true
195+
}
196+
}
197+
172198
// Create a serial queue for sample buffer processing
173199
let sampleBufferQueue = DispatchQueue(label: "com.michaelwolz.capacitorcameraview.snapshotQueue")
174200

175201
// Set the delegate for a single frame capture
176202
snapshotCompletionHandler = completion
177203
avVideoDataOutput.setSampleBufferDelegate(self, queue: sampleBufferQueue)
178-
179-
// Ensure proper orientation
180-
if let connection = avVideoDataOutput.connection(with: .video) {
181-
if connection.isVideoOrientationSupported {
182-
connection.videoOrientation = videoPreviewLayer.connection?.videoOrientation ?? .portrait
183-
}
184-
if connection.isVideoMirroringSupported && cameraDevice.position == .front {
185-
connection.isVideoMirrored = true
186-
}
187-
}
188204
}
189205

190206
public func setCameraById(_ cameraId: String) throws {
@@ -572,16 +588,16 @@ internal let SUPPORTED_CAMERA_DEVICE_TYPES: [AVCaptureDevice.DeviceType] = [
572588
return
573589
}
574590

575-
let orientation = UIDevice.current.orientation
591+
let interfaceOrientation = UIApplication.shared.windows.first?.windowScene?.interfaceOrientation ?? .portrait
576592
let videoOrientation: AVCaptureVideoOrientation
577593

578-
switch orientation {
594+
switch interfaceOrientation {
579595
case .portrait:
580596
videoOrientation = .portrait
581597
case .landscapeLeft:
582-
videoOrientation = .landscapeRight
583-
case .landscapeRight:
584598
videoOrientation = .landscapeLeft
599+
case .landscapeRight:
600+
videoOrientation = .landscapeRight
585601
case .portraitUpsideDown:
586602
videoOrientation = .portraitUpsideDown
587603
default:

0 commit comments

Comments
 (0)