ios - I have stopped captureSession but Scanner is yet working. How to resolve this bug? -
func scan(){ capturesession = avcapturesession() let videoinput: avcapturedeviceinput let videocapturedevice = avcapturedevice.defaultdevice(withmediatype: avmediatypevideo) let metadataoutput = avcapturemetadataoutput() { videoinput = try avcapturedeviceinput(device: videocapturedevice) } catch { return } if (capturesession.canaddinput(videoinput)) { capturesession.addinput(videoinput) } else { failed() return } if (capturesession.canaddoutput(metadataoutput)) { capturesession.addoutput(metadataoutput) metadataoutput.setmetadataobjectsdelegate(self, queue: dispatchqueue.main) metadataoutput.metadataobjecttypes = [avmetadataobjecttypeean8code, avmetadataobjecttypeean13code, avmetadataobjecttypepdf417code, avmetadataobjecttypecode128code, avmetadataobjecttypeitf14code, avmetadataobjecttypecode39code] } else { failed() return } var caplayer = self.previewlayer caplayer = avcapturevideopreviewlayer(session: capturesession); caplayer?.videogravity = avlayervideogravityresizeaspectfill caplayer?.frame = self.cameraview.layer.bounds self.cameraview.layer.addsublayer(caplayer!) capturesession.startrunning() } func failed() { let ac = uialertcontroller(title: "scanning not supported", message: "your device not support scanning code item. please use device camera.", preferredstyle: .alert) ac.addaction(uialertaction(title: "ok", style: .default, handler: nil)) present(ac, animated: true, completion: nil) capturesession = nil } func captureoutput(_ captureoutput: avcaptureoutput!, didoutputmetadataobjects metadataobjects: [any]!, connection: avcaptureconnection!) { capturesession.stoprunning() if let metadataobject: anyobject = metadataobjects.first anyobject? { let readableobject = metadataobject as! avmetadatamachinereadablecodeobject; audioservicesplaysystemsound(systemsoundid(ksystemsoundid_vibrate)) foundcode(readableobject.stringvalue) } if (capturesession?.isrunning == true) { capturesession.stoprunning() capturesession = nil } dismiss(animated: true, completion: nil) } func foundcode(_ code: string) { capturesession.stoprunning() storeid = (userdefaults.standard.value(forkey: defaultskey.storeid.rawvalue) as? int)! scannedcode = ("\(code)") if (capturesession?.isrunning == true) { capturesession.stoprunning() capturesession = nil } apicall() }
structure of app: triggering scan on click of button after successful api call. have stopped capture session once barcode found continuously scanning , sendung multiple api calls.
suggest how stop camera scanning without being triggered.
i guess @ point still stops , had same issue it. problem capture session uses queue, , when manages scan something, still able scan several times, until code stops it. why receive several delegate callbacks.
you can have 2 options here:
- make delegate
nil
when first receive callback , set again when want start.(though not sure work) - as did... introduce bool flag filter unwanted callbacks in delegate.
Comments
Post a Comment