gpt4 book ai didi

ios - 如何在 AVcapture session 中启动操作

转载 作者:行者123 更新时间:2023-11-30 12:00:57 24 4
gpt4 key购买 nike

我写了一个扫描条形码的代码。我想在扫描条形码时显示 View 。这是我的条形码扫描仪代码

    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/barcode is detected"
return
}
//Get metadata object
let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
if supportedCodeTypes.contains(metadataObj.type) {
//if the found metadata is equal to the QR code metadata then update the status label's text and set the the bounds
let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
qrCodeFrameView?.frame = barCodeObject!.bounds

if metadataObj.stringValue != nil {
var productbarcode = metadataObj.stringValue
messageLabel.text = productbarcode
print("Barcode detected")

}
}
}

我想要显示的 View 是在func Setupproductcontainer()中设置的。当我将其添加到 viewDidLoad 时它工作正常,但我不希望它在检测到条形码之前显示。因此,我尝试在 print("Barcode detector") 语句下添加 Setupproductcontainer() 但没有任何反应。当检测到条形码时,如何启动 Setupproductcontainer()

最佳答案

您可能正在尝试在后台线程上更改 UI。

我建议您在 DispatchQueue.main 代码块中调用您的函数以在主线程上运行它。您应该这样做的原因是对 UI 所做的每个更改都应该在主线程中完成。如果您尝试在后台线程中更改它,则不会发生任何事情。

DispatchQueue.main.async {
self.Setupproductcontainer()
}

关于ios - 如何在 AVcapture session 中启动操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47244026/

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