ios - fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) -
i using avfoundation in swift2 scan barcodes, code below. now, every randomly error:
fatal error: unexpectedly found nil while unwrapping optional value.
code:
// // placingorder.swift // sfc version2 // // created studiolaptop1 on 28/09/2015. // copyright (c) 2015 librex. rights reserved. // import uikit import avfoundation //making view controller implement delegate: avcapturemetadataoutputdelegate. class placingorder: uiviewcontroller, avcapturemetadataoutputobjectsdelegate { @iboutlet weak var thenav: uinavigationbar! @iboutlet weak var stopbutton: uibutton! //defining needed variables , constants let mysession : avcapturesession = avcapturesession() var thepreview : avcapturevideopreviewlayer! var hlview : uiview = uiview() //store string value of barcode var detectionstring : string! @ibaction func stoptapped(sender: uibutton) { self.mysession.stoprunning(); self.performseguewithidentifier("as1", sender: self) // // 1 // let optionmenu = uialertcontroller(title: nil, message: "choose option", preferredstyle: .actionsheet) // // // 2 // let exitaction = uialertaction(title: "exit", style: .default, handler: { // (alert: uialertaction!) -> void in // // let alertcontroller = uialertcontroller(title: "exit?", message: "are sure exit order process , go ?", preferredstyle: uialertcontrollerstyle.alert) // // let noaction = uialertaction(title: "no", style: uialertactionstyle.destructive, handler: nil) // alertcontroller.addaction(noaction) // // let okaction = uialertaction(title: "yes", style: uialertactionstyle.default, handler: {(alert :uialertaction!) in // self.mysession.stoprunning(); // self.performseguewithidentifier("as1", sender: self) // }) // alertcontroller.addaction(okaction) // // self.presentviewcontroller(alertcontroller, animated: true, completion: nil) // }) // let saveaction = uialertaction(title: "manual entry", style: .default, handler: { // (alert: uialertaction!) -> void in // // let alertcontroller = uialertcontroller(title: "manual entry?", message: "are sure manually enter item number?", preferredstyle: uialertcontrollerstyle.alert) // // let noaction = uialertaction(title: "no", style: uialertactionstyle.destructive, handler: nil) // alertcontroller.addaction(noaction) // // let okaction = uialertaction(title: "yes", style: uialertactionstyle.default, handler: {(alert :uialertaction!) in // self.mysession.stoprunning(); // self.performseguewithidentifier("as2", sender: self) // }) // alertcontroller.addaction(okaction) // // self.presentviewcontroller(alertcontroller, animated: true, completion: nil) // // }) // // // // let cancelaction = uialertaction(title: "cancel", style: .cancel, handler: { // (alert: uialertaction!) -> void in // print("cancelled"); // }) // // // // 4 // optionmenu.addaction(exitaction) // optionmenu.addaction(saveaction) // optionmenu.addaction(cancelaction) // // // 5 // self.presentviewcontroller(optionmenu, animated: true, completion: nil) } override func viewdidload() { super.viewdidload() //the uiview that’ll displayed once have successful scan. // allow view resize freely self.hlview.autoresizingmask = uiviewautoresizing.flexibletopmargin uiviewautoresizing.flexiblebottommargin uiviewautoresizing.flexibleleftmargin uiviewautoresizing.flexiblerightmargin // select color want completed scan reticle self.hlview.layer.bordercolor = uicolor.greencolor().cgcolor self.hlview.layer.borderwidth = 3 // add our controller's view subview. self.view.addsubview(self.hlview) /* device, input , output setup. set “device” default device video, camera. input camera sees, barcode if done correctly. if shows finger covering lens, doing wrong…unless have barcode on finger. output whatever capture found when scanning. output sets current view controller delegate.*/ //this camera let thedevice = avcapturedevice.defaultdevicewithmediatype(avmediatypevideo) // create nilable nserror hand off next method. //var error : nserror? = nil { let input : avcapturedeviceinput? = try avcapturedeviceinput(device: thedevice) avcapturedeviceinput mysession.addinput(input) } catch { let alertcontroller = uialertcontroller(title: "no input!", message: "looks scanner couldn't access camera! try again ensuring app has access camera, or why not enter product number manually.", preferredstyle: uialertcontrollerstyle.alert) let okaction = uialertaction(title: "ok", style: uialertactionstyle.default, handler: {(alert :uialertaction!) in //self.dismissviewcontrolleranimated(true, completion: nil) self.performseguewithidentifier("as2", sender: self) }) alertcontroller.addaction(okaction) presentviewcontroller(alertcontroller, animated: true, completion: nil) } // if our input not nil add session, otherwise we're kind of done! //if input != nil { //mysession.addinput(input) //} //else { // let alert = uialertview() // alert.title = "no input!" // alert.message = "looks scanner couldn't access camera! try again ensuring app has access camera, or why not place order manually." // alert.addbuttonwithtitle("ok") // alert.show() // self.dismissviewcontrolleranimated(true, completion: nil) // let alertcontroller = uialertcontroller(title: "no input!", message: "looks scanner couldn't access camera! try again ensuring app has access camera, or why not place order manually.", preferredstyle: uialertcontrollerstyle.alert) // // let okaction = uialertaction(title: "ok", style: uialertactionstyle.default, handler: {(alert :uialertaction!) in // self.dismissviewcontrolleranimated(true, completion: nil) // }) // alertcontroller.addaction(okaction) // // presentviewcontroller(alertcontroller, animated: true, completion: nil) //} let output = avcapturemetadataoutput() output.setmetadataobjectsdelegate(self, queue: dispatch_get_main_queue()) mysession.addoutput(output) output.metadataobjecttypes = output.availablemetadataobjecttypes // calculate centered square rectangle red border let size = 200 let screenwidth = self.view.frame.size.width let screenheight = self.view.frame.size.height let xpos = (cgfloat(screenwidth) / cgfloat(2)) - (cgfloat(size) / cgfloat(2)) let ypos = (cgfloat(screenheight) / cgfloat(2)) - (cgfloat(size) / cgfloat(2)) let scanrect = cgrect(x: int(xpos), y: int(ypos), width: size, height: size) // create uiview server red square indicate place qrcode scanning let scanareaview = uiview() scanareaview.layer.bordercolor = uicolor.redcolor().cgcolor scanareaview.layer.borderwidth = 3; scanareaview.frame = scanrect let imagename = "download.png" let image = uiimage(named: imagename) let imageview = uiimageview(image: image!) imageview.frame = scanrect //displaying camera sees thepreview = avcapturevideopreviewlayer(session: mysession) avcapturevideopreviewlayer //setting frame view controller’s size thepreview.frame = self.view.bounds //setting how preview displayed inside frame thepreview.videogravity = avlayervideogravityresizeaspectfill //adding preview view layer sublayer self.view.layer.addsublayer(thepreview) self.view.bringsubviewtofront(self.stopbutton) self.view.bringsubviewtofront(self.thenav) view.addsubview(scanareaview) view.addsubview(imageview) // starting scanner. mysession.startrunning() } // function called when known barcode type found camera. func captureoutput(captureoutput: avcaptureoutput!, didoutputmetadataobjects metadataobjects: [anyobject]!, fromconnection connection: avcaptureconnection!) { //a cgrect that’ll serve dimensions captured reticle, barcode, parsed string , allowed barcode types. var highlightviewrect = cgrectzero var barcodeobject : avmetadataobject! //allowed barcode types let barcodetypes = [avmetadataobjecttypeupcecode, avmetadataobjecttypecode39code, avmetadataobjecttypecode39mod43code, avmetadataobjecttypeean13code, avmetadataobjecttypeean8code, avmetadataobjecttypecode93code, avmetadataobjecttypecode128code, avmetadataobjecttypepdf417code, avmetadataobjecttypeqrcode, avmetadataobjecttypeazteccode ] //looping on metadataobjects passed method, , determining if captured barcode 1 of our allowed types. if parse string value out set capture reticle , stop scanner. metadata in metadataobjects { barcodetype in barcodetypes { if metadata.type == barcodetype { barcodeobject = self.thepreview.transformedmetadataobjectformetadataobject(metadata as! avmetadatamachinereadablecodeobject) highlightviewrect = barcodeobject.bounds detectionstring = (metadata as! avmetadatamachinereadablecodeobject).stringvalue self.mysession.stoprunning() break } } } //showing readable barcode print(detectionstring) self.hlview.frame = highlightviewrect self.view.bringsubviewtofront(self.hlview) // pass identifier - go recieving view controller. self.performseguewithidentifier("itemnofororderscanned", sender: self) } override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if segue.identifier == "itemnofororderscanned" { //get reference - ...to receiver view. let nextview = segue.destinationviewcontroller as? placingorderstep2 //pass stuff - pass variable along target. nextview!.scanneditem = self.detectionstring } } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } /* // mark: - navigation // in storyboard-based application, want little preparation before navigation override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { // new view controller using segue.destinationviewcontroller. // pass selected object new view controller. } */ }
in thread 0 0x3e4338 <+72>: trap
Comments
Post a Comment