当我尝试使用qr库扫描qr代码并运行视图控制器时,它可以工作3秒钟,然后挂起,但是在其他应用程序中实现相同功能时,效果很好
import UIKit
import AVFoundation
import QRCodeReader
class QRScannerViewController: UIViewController , QRCodeReaderViewControllerDelegate{
// Good practice: create the reader lazily to avoid cpu overload during the
// initialization and each time we need to scan a QRCode
lazy var readerVC: QRCodeReaderViewController = {
let builder = QRCodeReaderViewControllerBuilder {
$0.reader = QRCodeReader(metadataObjectTypes: [.qr], captureDevicePosition: .back)
// Configure the view controller (optional)
$0.showTorchButton = false
$0.showSwitchCameraButton = false
$0.showCancelButton = true
$0.showOverlayView = true
$0.rectOfInterest = CGRect(x: 0.2, y: 0.2, width: 0.6, height: 0.6)
}
return QRCodeReaderViewController(builder: builder)
}()
@IBAction func scanAction(_ sender: AnyObject) {
// Retrieve the QRCode content
// By using the delegate pattern
readerVC.delegate = self
// Or by using the closure pattern
readerVC.completionBlock = { (result: QRCodeReaderResult?) in
print(result)
}
// Presents the readerVC as modal form sheet
readerVC.modalPresentationStyle = .formSheet
present(readerVC, animated: true, completion: nil)
}
// MARK: - QRCodeReaderViewController Delegate Methods
public func reader(_ reader: QRCodeReaderViewController, didScanResult result: QRCodeReaderResult) {
reader.stopScanning()
dismiss(animated: true, completion: nil)
}
//This is an optional delegate method, that allows you to be notified when the user switches the cameraName
//By pressing on the switch camera button
public func reader(_ reader: QRCodeReaderViewController, didSwitchCamera newCaptureDevice: AVCaptureDeviceInput) {
// if let cameraName = newCaptureDevice.device.localizedName as? String{
// print("Switching capture to: \(cameraName)")
// } else {
// return
// }
}
public func readerDidCancel(_ reader: QRCodeReaderViewController) {
reader.stopScanning()
dismiss(animated: true, completion: nil)
}
public override func viewDidLoad() {
super.viewDidLoad()
}
}