ios - How to properly end an AVCaptureSession? -
i have app qr code reader. set scanner in viewwillappear:
// create new avcapturesession. _session = [avcapturesession new]; avcapturedevice *device = [avcapturedevice defaultdevicewithmediatype:avmediatypevideo]; nserror *error = nil; avcapturedeviceinput *input = [avcapturedeviceinput deviceinputwithdevice:device error:&error]; // connect input. if(input) { [_session addinput:input]; } else { nslog(@"error: %@", error); return; } // connect output. avcapturemetadataoutput *output = [avcapturemetadataoutput new]; [_session addoutput:output]; nslog(@"%@", [output availablemetadataobjecttypes]); [output setmetadataobjecttypes:@[avmetadataobjecttypeqrcode]]; [output setmetadataobjectsdelegate:self queue:dispatch_get_main_queue()]; // connect preview layer. _previewlayer = [avcapturevideopreviewlayer layerwithsession:_session]; _previewlayer.videogravity = avlayervideogravityresizeaspectfill; _previewlayer.bounds = self.view.bounds; _previewlayer.position = cgpointmake(cgrectgetmidx(self.view.bounds), cgrectgetmidy(self.view.bounds)); [self.view.layer addsublayer:_previewlayer]; // start avsession. [_session startrunning];
i implement delegate method so:
- (void)captureoutput:(avcaptureoutput *)captureoutput didoutputmetadataobjects:(nsarray *)metadataobjects fromconnection:(avcaptureconnection *)connection { // iterate through visible metadata objects. (avmetadataobject *metadata in metadataobjects) { // care qr codes. if ([metadata.type isequaltostring:avmetadataobjecttypeqrcode]) { // parse qr code. avmetadatamachinereadablecodeobject *transformed = (avmetadatamachinereadablecodeobject *)[_previewlayer transformedmetadataobjectformetadataobject:metadata]; // stop session. [_previewlayer removefromsuperlayer]; [_session stoprunning]; // other stuff, push next view controller.
unfortunately, when try run this, continues scanning qr code multiple times after call stop. after navigate away see bunch of errors saying, "finishing navigation transition in unexpected state. navigation bar subview tree might corrupted." , crashes. there i'm missing here?
i had similar issue too. hardware directly related features grabbed data goes in buffer. experiencing after stopping session last frames grabbed camera , put queue. solution (quick , dirty) create bool variable (static or ivar) change states right before stop running , check state @ beginning of delegate method.
don't have doc in hand i'm pretty sure can ask session directly states running
property , interrupted
property
Comments
Post a Comment