QRcode 扫描后我的解雇不起作用怎么办?

What should I do if my dismiss does not work after a QRcode scan?

我要扫描 webView 中的 QR code

QR code扫描好,可以读取数据,

但问题是,扫描后相机屏幕不会关闭。我是 运行 一个 dismiss() 函数。

webView 加载

@IBOutlet weak var indicator: UIImageView!
@IBOutlet var wkWebView: WKWebView!
...
 let config = WKWebViewConfiguration()

        contentController.add(self, name: "native")

        config.userContentController = contentController

        wkWebView = WKWebView(frame: wkWebView.frame, configuration: config)

        wkWebView.uiDelegate = self
        wkWebView.navigationDelegate = self

        view.addSubview(wkWebView)
        view.addSubview(indicator)
let localFilePath = Bundle.main.url(forResource: webUrl, withExtension: "html")
let myRequest = URLRequest(url: localFilePath)
wkWebView.load(myRequest)

二维码扫描

var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!

   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 qrcodeScan(){
        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 = [.qr]
        } else {
            failed()
            return
        }

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

        captureSession.startRunning()
    }

    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)
        }

        self.dismiss(animated: true, completion: nil)
    }

    func found(code: String) {
        Log.Info(code)
    }


    func failed() {
        captureSession = nil
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }


    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }

正如您从我的代码中看到的那样,我试图在读取 QR code 后将相机 window 放下。

但是相机画面还在卡屏。无论我等多久,我都看不到网络视图屏幕。有什么问题吗?

previewLayer 占用了所有屏幕(边界)并且它隐藏了 webview,因此您应该从超级层中删除 previewLayer 以显示 webview。

所以解决方案是使用这个:

previewLayer.removeFromSuperlayer()

而不是

self.dismiss(animated: true, completion: nil)