gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:24:36 27 4
gpt4 key购买 nike

我要扫描 webView 中的 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 码 后将摄像头窗口放下。

但相机屏幕仍然在卡住屏幕上。无论我等多久,我都看不到 WebView 屏幕。有什么问题?

最佳答案

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

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

previewLayer.removeFromSuperlayer()

代替

self.dismiss(动画:true,完成:nil)

关于ios - QRcode 扫描后我的解雇不起作用怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58603707/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com