簡體   English   中英

使用RSBarcodes掃描條形碼時執行操作

[英]Perform Action When a Barcode Is Scanned Using RSBarcodes

我正在構建一個使用RSBarcodes for Swift進行 QR碼掃描的應用程序。 我在ScanViewController嘗試做的是掃描QR碼,驗證掃描的內容,然后掃描掃描的數據。 目前,當檢測到QR代碼時,我的UI凍結,並且在我收到錯誤和內存轉儲后不久:

'NSInternalInconsistencyException',原因:'只在主線程上運行!'。

也許這不是驗證QR碼的正確位置,或者不適合segue,但如果沒有,我想知道驗證和segue應該在哪里進行。 我唯一的另一個要求是驗證僅在檢測到QR碼時才會發生。

class ScanViewController: RSCodeReaderViewController{
    // Class Variables
    var finalObject: IBuiltCode?
    let ObjectHelper = ObjectBuilder() // Service to validate and build valid scanned objects

    override func viewDidLoad() {
        super.viewDidLoad()

        self.focusMarkLayer.strokeColor = UIColor.redColor().CGColor
        self.cornersLayer.strokeColor = UIColor.yellowColor().CGColor

        self.tapHandler = { point in
            println(point)
        }

        self.barcodesHandler = { barcodes in
            for barcode in barcodes {
                println("Barcode found: type=" + barcode.type + " value=" + barcode.stringValue)
                if let builtObject = self.ObjectHelper.validateAndBuild(barcode,
                      scannedData: barcode.stringValue){
                    println("Good object.")
                    self.performQR()
                }
            }
        }
    }

    func performQR(){
        performSegueWithIdentifier("toQR", sender: self)
    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "toQR"){
            let QRVC: QRViewController = segue.destinationViewController as! QRViewController
            QRVC.receivedObject = finalObject as? QRObject
        }
    }
}

我在這個問題線程上聯系了RSBarcodes_Swift的開發人員。 為了執行任何UI操作,需要在主線程上運行。 例如,需要更改segue函數:

func performQR(){
        self.performSegueWithIdentifier("toQR", sender: self)
}

func performQR(){
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        self.performSegueWithIdentifier("toQR", sender: self)
    })
}

為了避免在掃描時多次進行self.session.stopRunning() ,可以在barcodes for循環中使用調用self.session.stopRunning()break

self.barcodesHandler = { barcodes in
    for barcode in barcodes {
        println("Barcode found: type=" + barcode.type + " value=" + barcode.stringValue)
        if let builtObject = self.ObjectHelper.validateAndBuild(barcode,
              scannedData: barcode.stringValue){
            println("Good object.")
            self.finalObject = builtObject
            self.session.stopRunning() // Avoid scanning multiple times
            self.performQR()
            break
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM