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
## [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)
Release on 2018-17-02

View File

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

View File

@ -145,7 +145,7 @@ final public class QRCodeReaderView: UIView, QRCodeReaderDisplayable {
}
}
@objc func orientationDidChange() {
@objc public func setNeedsUpdateOrientation() {
setNeedsDisplay()
overlayView?.setNeedsDisplay()
@ -162,7 +162,7 @@ final public class QRCodeReaderView: UIView, QRCodeReaderDisplayable {
// MARK: - Convenience Methods
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)
@ -183,17 +183,16 @@ final public class QRCodeReaderView: UIView, QRCodeReaderDisplayable {
}
if let reader = reader {
print("reader", reader.previewLayer)
cameraView.layer.insertSublayer(reader.previewLayer, at: 0)
orientationDidChange()
setNeedsUpdateOrientation()
}
}
}
extension QRCodeReaderView: QRCodeReaderLifeCycleDelegate {
func readerDidStartScanning() {
orientationDidChange()
setNeedsUpdateOrientation()
}
func readerDidStopScanning() {}

View File

@ -43,6 +43,9 @@ public protocol QRCodeReaderDisplayable {
/// A guide view upon the camera view
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.

View File

@ -112,6 +112,8 @@ public class QRCodeReaderViewController: UIViewController {
super.viewWillAppear(animated)
if startScanningAtLoad {
readerView.displayable.setNeedsUpdateOrientation()
startScanning()
}
}
@ -148,7 +150,7 @@ public class QRCodeReaderViewController: UIViewController {
// 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))
}
}