我正在建立一个应用程序,利用QR代码扫描使用Swift的RSBarcodes。在我的ScanViewController中,我要做的是扫描QR代码,验证所扫描的内容,然后用所扫描的数据进行验证。当前,当检测到QR代码时,我的UI会冻结,并且在我得到一个错误和内存转储之后不久:
“‘NSInternalInconsistencyException”,原因:“只在主线程上运行!”。
也许这里不是验证QR代码的合适位置,也不是验证QR代码的合适位置,但如果不是,我想知道验证和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
}
}
}发布于 2015-07-20 16:51:13
我在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()循环中使用调用break和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
}
}
}https://stackoverflow.com/questions/31521973
复制相似问题