Fixing the scan orienation when view will dislay #123

This commit is contained in:
Yannick Loriot 2018-04-03 19:07:10 +02:00
parent b11893ed75
commit 4bf1eeaaa8
5 changed files with 19 additions and 7 deletions

View File

@ -1,5 +1,10 @@
# Change log # Change log
## [Version 8.2.0](https://github.com/yannickl/QRCodeReader.swift/releases/tag/8.2.0)
Release on 2018-04-03
- Fixing the camera orientation when view is display (#123).
## [Version 8.1.1](https://github.com/yannickl/QRCodeReader.swift/releases/tag/8.1.1) ## [Version 8.1.1](https://github.com/yannickl/QRCodeReader.swift/releases/tag/8.1.1)
Release on 2018-17-02 Release on 2018-17-02

View File

@ -126,7 +126,8 @@ public final class QRCodeReader: NSObject, AVCaptureMetadataOutputObjectsDelegat
*/ */
public init(metadataObjectTypes types: [AVMetadataObject.ObjectType], captureDevicePosition: AVCaptureDevice.Position) { public init(metadataObjectTypes types: [AVMetadataObject.ObjectType], captureDevicePosition: AVCaptureDevice.Position) {
metadataObjectTypes = types metadataObjectTypes = types
previewLayer = AVCaptureVideoPreviewLayer(session: session) previewLayer = AVCaptureVideoPreviewLayer(session: session)
super.init() super.init()
sessionQueue.async { sessionQueue.async {
@ -159,10 +160,12 @@ public final class QRCodeReader: NSObject, AVCaptureMetadataOutputObjectsDelegat
// Add metadata output // Add metadata output
session.addOutput(metadataOutput) session.addOutput(metadataOutput)
metadataOutput.setMetadataObjectsDelegate(self, queue: metadataObjectsQueue) metadataOutput.setMetadataObjectsDelegate(self, queue: metadataObjectsQueue)
let allTypes = Set(metadataOutput.availableMetadataObjectTypes) let allTypes = Set(metadataOutput.availableMetadataObjectTypes)
let filtered = metadataObjectTypes.filter { (mediaType) -> Bool in let filtered = metadataObjectTypes.filter { (mediaType) -> Bool in
allTypes.contains(mediaType) allTypes.contains(mediaType)
} }
metadataOutput.metadataObjectTypes = filtered metadataOutput.metadataObjectTypes = filtered
previewLayer.videoGravity = .resizeAspectFill previewLayer.videoGravity = .resizeAspectFill

View File

@ -145,7 +145,7 @@ final public class QRCodeReaderView: UIView, QRCodeReaderDisplayable {
} }
} }
@objc func orientationDidChange() { @objc public func setNeedsUpdateOrientation() {
setNeedsDisplay() setNeedsDisplay()
overlayView?.setNeedsDisplay() overlayView?.setNeedsDisplay()
@ -162,7 +162,7 @@ final public class QRCodeReaderView: UIView, QRCodeReaderDisplayable {
// MARK: - Convenience Methods // MARK: - Convenience Methods
private func addComponents() { private func addComponents() {
NotificationCenter.default.addObserver(self, selector: #selector(QRCodeReaderView.orientationDidChange), name: .UIDeviceOrientationDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(self.setNeedsUpdateOrientation), name: .UIDeviceOrientationDidChange, object: nil)
addSubview(cameraView) addSubview(cameraView)
@ -183,17 +183,16 @@ final public class QRCodeReaderView: UIView, QRCodeReaderDisplayable {
} }
if let reader = reader { if let reader = reader {
print("reader", reader.previewLayer)
cameraView.layer.insertSublayer(reader.previewLayer, at: 0) cameraView.layer.insertSublayer(reader.previewLayer, at: 0)
orientationDidChange() setNeedsUpdateOrientation()
} }
} }
} }
extension QRCodeReaderView: QRCodeReaderLifeCycleDelegate { extension QRCodeReaderView: QRCodeReaderLifeCycleDelegate {
func readerDidStartScanning() { func readerDidStartScanning() {
orientationDidChange() setNeedsUpdateOrientation()
} }
func readerDidStopScanning() {} func readerDidStopScanning() {}

View File

@ -43,6 +43,9 @@ public protocol QRCodeReaderDisplayable {
/// A guide view upon the camera view /// A guide view upon the camera view
var overlayView: UIView? { get } var overlayView: UIView? { get }
/// Notify the receiver to update its orientation.
func setNeedsUpdateOrientation()
/** /**
Method called by the container to allows you to layout your view properly using the given flags. Method called by the container to allows you to layout your view properly using the given flags.

View File

@ -112,6 +112,8 @@ public class QRCodeReaderViewController: UIViewController {
super.viewWillAppear(animated) super.viewWillAppear(animated)
if startScanningAtLoad { if startScanningAtLoad {
readerView.displayable.setNeedsUpdateOrientation()
startScanning() startScanning()
} }
} }
@ -148,7 +150,7 @@ public class QRCodeReaderViewController: UIViewController {
// Setup constraints // Setup constraints
for attribute in [NSLayoutAttribute.left, NSLayoutAttribute.top, NSLayoutAttribute.right, NSLayoutAttribute.bottom] { for attribute in [.left, .top, .right, .bottom] as [NSLayoutAttribute] {
view.addConstraint(NSLayoutConstraint(item: readerView.view, attribute: attribute, relatedBy: .equal, toItem: view, attribute: attribute, multiplier: 1, constant: 0)) view.addConstraint(NSLayoutConstraint(item: readerView.view, attribute: attribute, relatedBy: .equal, toItem: view, attribute: attribute, multiplier: 1, constant: 0))
} }
} }