【问题标题】:How to initiate an action in AVcapture Session如何在 AVcapture 会话中启动操作
【发布时间】:2018-04-24 22:29:00
【问题描述】:

我编写了一个用于扫描条形码的代码。我想在扫描条形码后显示一个视图。这是我的条码扫描器代码

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

        }
    }
}

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

【问题讨论】:

  • 您使用的是哪个 Swift 版本?您确定正在调用委托方法吗?

标签: ios swift


【解决方案1】:

您可能正试图在后台线程上进行 UI 更改。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-07
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    • 2019-02-14
    • 1970-01-01
    相关资源
    最近更新 更多