条形码扫描后屏幕冻结

After a barcode scan the screen freezes

我正在学习 SWIFT 并且越来越好。我的条形码应用程序可以工作,但它会无休止地扫描条形码,直到我强制弹出屏幕 'Ok'、'Cancel' 来接受或拒绝扫描。在研究中,我发现了非常相似的代码(如下所示)。所以我创建了一个只有一个视图控制器的测试项目来尝试这段代码。它工作得很好,但一旦扫描条形码就会冻结。我的意图是解冻屏幕,接受并保存它并允许用户扫描更多条形码。任何帮助是极大的赞赏。我尝试在 found(code:) 行下方停止扫描,但它无助于解冻。我可能是错的,但我怀疑 viewilldisapper 永远不会在这里被调用。


import AVFoundation
import UIKit

class ScannerViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = UIColor.black
    captureSession = AVCaptureSession()

    guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else { return }
    let videoInput: AVCaptureDeviceInput

    do {
        videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
    } catch {
        return
    }

    if (captureSession.canAddInput(videoInput)) {
        captureSession.addInput(videoInput)
    } else {
        failed()
        return
    }

    let metadataOutput = AVCaptureMetadataOutput()

    if (captureSession.canAddOutput(metadataOutput)) {
        captureSession.addOutput(metadataOutput)

        metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
        metadataOutput.metadataObjectTypes = [.ean8, .ean13, .pdf417]
    } else {
        failed()
        return
    }

    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    previewLayer.frame = view.layer.bounds
    previewLayer.videoGravity = .resizeAspectFill
    view.layer.addSublayer(previewLayer)

    captureSession.startRunning()
}

func failed() {
    let ac = UIAlertController(title: "Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.", preferredStyle: .alert)
    ac.addAction(UIAlertAction(title: "OK", style: .default))
    present(ac, animated: true)
    captureSession = nil
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if (captureSession?.isRunning == false) {
        captureSession.startRunning()
    }
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    if (captureSession?.isRunning == true) {
        captureSession.stopRunning()
    }
}

func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
    captureSession.stopRunning()

    if let metadataObject = metadataObjects.first {
        guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
        guard let stringValue = readableObject.stringValue else { return }
        AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
        found(code: stringValue)
    }

    dismiss(animated: true)
}

func found(code: String) {
    print(code)
}

override var prefersStatusBarHidden: Bool {
    return true
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}

}


编辑代码:

 if let metadataObject = metadataObjects.first {
        guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
        guard let stringValue = readableObject.stringValue else { return }
        AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
        found(code: stringValue)

        barcodeValue = stringValue
    }

    // dismiss(animated: true)
    previewLayer.removeFromSuperlayer()

    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    previewLayer.frame = view.layer.bounds
    previewLayer.videoGravity = .resizeAspectFill
    view.layer.addSublayer(previewLayer)

    let alertController = UIAlertController(title: "Barcode Scanned", message: barcodeValue!, preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: {(alert: UIAlertAction!) in self.restartScan()}))
    alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:nil))
    present(alertController, animated: true, completion: nil)
    //captureSession.startRunning()
}

func found(code: String) {
    print(code)
}

func restartScan() {
 captureSession.startRunning()
}

这不是冻结,当检测到第一个条形码时,您会在 func metadataOutput(_ output: AVCaptureMetadataOutput 内使用 captureSession.stopRunning() 停止进程,您需要这个

previewLayer.removeFromSuperlayer()

而不是这个

dismiss(animated: true)

因为它是一个图层而不是 vc 可以忽略您之前在

中添加的图层
view.layer.addSublayer(previewLayer)

注意:您的视图背景为黑色