Skip to content

Commit

Permalink
Fixed orientation on iPads when starting in landscape mode (#565)
Browse files Browse the repository at this point in the history
Video connection was not ready, so rotation would silently fail
  • Loading branch information
scarlac committed Jul 25, 2023
1 parent 289696d commit 4892d53
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions ios/ReactNativeCameraKit/RealCamera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
DispatchQueue.main.async {
self.cameraPreview.session = self.session
self.cameraPreview.previewLayer.videoGravity = .resizeAspect
self.setVideoOrientationToInterfaceOrientation()
}

self.initializeMotionManager()
Expand All @@ -114,6 +113,10 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
// We need to reapply the configuration after starting the camera
self.update(torchMode: self.torchMode)
}

DispatchQueue.main.async {
self.setVideoOrientationToInterfaceOrientation()
}
}
}

Expand Down Expand Up @@ -541,7 +544,7 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
motionManager = CMMotionManager()
motionManager?.accelerometerUpdateInterval = 0.2
motionManager?.gyroUpdateInterval = 0.2
motionManager?.startAccelerometerUpdates(to: OperationQueue(), withHandler: { [weak self] (accelerometerData, error) -> Void in
motionManager?.startAccelerometerUpdates(to: OperationQueue(), withHandler: { (accelerometerData, error) -> Void in
guard error == nil else {
print("\(error!)")
return
Expand All @@ -551,13 +554,13 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
return
}

guard let newOrientation = self?.deviceOrientation(from: accelerometerData.acceleration),
newOrientation != self?.deviceOrientation else {
guard let newOrientation = self.deviceOrientation(from: accelerometerData.acceleration),
newOrientation != self.deviceOrientation else {
return
}

self?.deviceOrientation = newOrientation
self?.onOrientationChange?(["orientation": Orientation.init(from: newOrientation)!.rawValue])
self.deviceOrientation = newOrientation
self.onOrientationChange?(["orientation": Orientation.init(from: newOrientation)!.rawValue])
})
}

Expand Down

0 comments on commit 4892d53

Please sign in to comment.