2017-10-20 153 views
0

我的朋友和我最近从swift 3.2更新到swift 4.不幸的是,我们的项目不再正常运行我们的QR阅读器功能。视频输出工作正常,所以当我们加载页面时,屏幕会在手机看到时显示。但是,它将不再将尺寸框放在屏幕上或识别前面的QR码。从swift 4中断更新应用程序

import UIKit 
    import AVFoundation 
    class ScanViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate { 

    @IBOutlet var messageLabel:UILabel! 
    @IBOutlet var topbar: UIView! 

    var captureSession: AVCaptureSession? 
    var videoPreviewLayer: AVCaptureVideoPreviewLayer? 
    var qrCodeFrameView: UIView? 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Get an instance of the AVCaptureDevice class to initialize a device object and provide the video as the media type parameter. 
     let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) 

     do { 
      // Get an instance of the AVCaptureDeviceInput class using the previous device object. 
      let input = try AVCaptureDeviceInput(device: captureDevice) 

      // Initialize the captureSession object. 
      captureSession = AVCaptureSession() 

      // Set the input device on the capture session. 
      captureSession?.addInput(input) 

      // Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session. 
      let captureMetadataOutput = AVCaptureMetadataOutput() 
      captureSession?.addOutput(captureMetadataOutput) 

      // Set delegate and use the default dispatch queue to execute the call back 
      captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main) 
      captureMetadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode] 

      // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer. 
      videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 
      videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill 
      videoPreviewLayer?.frame = view.layer.bounds 
      view.layer.addSublayer(videoPreviewLayer!) 

      // Move the message label and top bar to the front 
      view.bringSubview(toFront: messageLabel) 
      view.bringSubview(toFront: topbar) 

      // Start video capture. 
      captureSession?.startRunning() 

      // Initialize QR Code Frame to highlight the QR code 
      qrCodeFrameView = UIView() 

      if let qrCodeFrameView = qrCodeFrameView { 
       qrCodeFrameView.layer.borderColor = UIColor.randomColor().cgColor 
       qrCodeFrameView.layer.borderWidth = 4 
       view.addSubview(qrCodeFrameView) 
       view.bringSubview(toFront: qrCodeFrameView) 
      } 

     } catch { 
      // If any error occurs, simply print it out and don't continue any more. 
      print(error) 
      return 
     } 
    } 

    func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) { 

     // Check if the metadataObjects array is not nil and it contains at least one object. 
     if metadataObjects == nil || metadataObjects.count == 0 { 
      qrCodeFrameView?.frame = CGRect.zero 
      messageLabel.text = "No QR code is detected" 
      return 
     } 

     // Get the metadata object. 
     let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject 

     if metadataObj.type == AVMetadataObjectTypeQRCode { 
      // If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds 
      let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj) 
      qrCodeFrameView?.frame = barCodeObject!.bounds 

      if metadataObj.stringValue != nil { //Output 
       messageLabel.text = metadataObj.stringValue 

       performSegue(withIdentifier: "QRFound", sender: self) 
      } 
     } 
    } 

    @IBAction func cancel(_ sender: UIButton) { 
     if let owningNavController = navigationController { 
      owningNavController.popViewController(animated: true) 
     } 
     dismiss(animated: true, completion: nil) 
    } 
} 
+1

如果您提供了更多关于您尝试过和看到的事情的细节(屏幕和调试器),我会帮上忙。 –

回答

0

您的委托函数是不正确的,所以它永远不会被调用。正确的是:

optional func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) 

请参阅 documentation page。 这似乎在Swift 4中发生了变化。

编辑:另请参阅this answer