ios metadataOutput功能覆盖导航栏

时间:2018-04-07 14:23:16

标签: ios uiviewcontroller

我有以下控制器:

import UIKit
import AVFoundation

class ScanViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        //Creating session
        let session = AVCaptureSession()

        // Get the back-facing camera for capturing videos
        let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)

        do{
            let input = try AVCaptureDeviceInput(device: captureDevice!)
            session.addInput(input)
        }
        catch {
            print("ERROR")
        }

        let output = AVCaptureMetadataOutput()
        session.addOutput(output)
        output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
        output.metadataObjectTypes = [AVMetadataObject.ObjectType.qr, AVMetadataObject.ObjectType.code39, AVMetadataObject.ObjectType.code128]

        let video = AVCaptureVideoPreviewLayer(session: session)
        video.frame=view.layer.bounds
        //view.layer.addSublayer(video)
        view.layer.insertSublayer(video, below: view.layer  )
        session.startRunning()

    }

    func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
        if metadataObjects != nil && metadataObjects.count != 0 {
            if let object = metadataObjects[0] as? AVMetadataMachineReadableCodeObject {
                if object.type == AVMetadataObject.ObjectType.qr {
                    showAlert(object: object)
                    //TODO: export QR code
                }
                if object.type == AVMetadataObject.ObjectType.code39 {
                    showAlert(object: object)
                    //TODO: process code39
                }
                if object.type == AVMetadataObject.ObjectType.code128 {
                    showAlert(object: object)
                    //TODO: process code128
                }
            }


        }
    }

直到session.startRunning()我才能看到导航栏。 然后func metadataOutput幻灯片全帧图像。条码扫描工作正常,只有我看不到我的导航栏。 在debbuging期间,一旦开始识别metadataObjectTypes,我的导航栏就会被覆盖。 有什么建议吗?

1 个答案:

答案 0 :(得分:0)

嵌入导航控制器解决问题。 然后在导航窗格中创建seague和按钮来实现技巧

@IBAction func backButtonPressed(_ sender: UIButton) {
        self.performSegue(withIdentifier: "showHomeSegue", sender: self)
    }