Skip to content

Commit

Permalink
Task: Fixing rotated preview layer on a landscape oriented iPad
Browse files Browse the repository at this point in the history
Signed-off-by: Vikas Chandra <[email protected]>

Closes: #42
  • Loading branch information
Ivan Yavorin authored and vikas-chandra-mnnit committed Apr 26, 2022
1 parent 715527f commit 09927af
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion ESPProvision/ESPProvisionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,25 @@ public class ESPProvisionManager: NSObject, AVCaptureMetadataOutputObjectsDelega
return
}

self.previewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)
let aPreviewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)

if aPreviewLayer.connection?.isVideoOrientationSupported ?? false {

var interfaceOrientation:UIInterfaceOrientation = .portrait
var videoOrientation:AVCaptureVideoOrientation = .portrait

if #available(iOS 13.0, *) {
interfaceOrientation = UIApplication.shared.windows.first?.windowScene?.interfaceOrientation ?? .portrait

} else {
interfaceOrientation = UIApplication.shared.statusBarOrientation
}

videoOrientation = interfaceOrientation.videoOrientation ?? videoOrientation
aPreviewLayer.connection?.videoOrientation = videoOrientation
}

self.previewLayer = aPreviewLayer
self.previewLayer?.frame = scanView.layer.bounds
self.previewLayer?.videoGravity = .resizeAspectFill
scanView.layer.addSublayer(self.previewLayer!)
Expand Down Expand Up @@ -319,3 +337,15 @@ extension ESPProvisionManager: ESPBLETransportDelegate {
self.scanCompletionHandler?(nil,.espDeviceNotFound)
}
}

extension UIInterfaceOrientation {
var videoOrientation: AVCaptureVideoOrientation? {
switch self {
case .portraitUpsideDown: return .portraitUpsideDown
case .landscapeRight: return .landscapeRight
case .landscapeLeft: return .landscapeLeft
case .portrait: return .portrait
default: return nil
}
}
}

0 comments on commit 09927af

Please sign in to comment.