iphone - AVCaptureSession fails when returning from background -


i have camera preview window working 90% of time. however, when returning app if it's been in background, preview not display. code call when view loads:

- (void) startcamera {  session = [[avcapturesession alloc] init]; session.sessionpreset = avcapturesessionpresetphoto;  avcapturevideopreviewlayer *capturevideopreviewlayer = [[avcapturevideopreviewlayer alloc] initwithsession:session]; capturevideopreviewlayer.frame = _cameraview.bounds; [_cameraview.layer addsublayer:capturevideopreviewlayer]; capturevideopreviewlayer.videogravity = avlayervideogravityresizeaspectfill; capturevideopreviewlayer.position=cgpointmake(cgrectgetmidx(_cameraview.bounds), 160);  avcapturedevice *device = [avcapturedevice defaultdevicewithmediatype:avmediatypevideo]; nserror *error = nil;  avcapturedeviceinput *input = [avcapturedeviceinput deviceinputwithdevice:device error:&error]; if (!input) {      nslog(@"error: %@", error);       uialertview *alert = [[uialertview alloc] initwithtitle:@"important!"                                                     message:@"unable find camera."                                                    delegate:nil                                           cancelbuttontitle:@"ok"                                           otherbuttontitles:nil];     [alert show];     [alert autorelease]; }  [session addinput:input];  stillimage = [[avcapturestillimageoutput alloc] init]; nsdictionary *outputsettings = [[nsdictionary alloc] initwithobjectsandkeys:avvideocodecjpeg , avvideocodeckey, nil]; [stillimage setoutputsettings:outputsettings];  [session addoutput:stillimage]; [session startrunning]; } 

if happens, can switch preferences view , again , al well,but it's annoying bug i'd kill. preview window uiview in storyboard.

do not start capture session on view load, instead start on viewwillappear , stop on viewwilldissapear.

seems view controller cleaning memory when app in background. make sure initializing capture session in mind.

allocate session lazily in private property getter method rather in start method avoid memory leaks way.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -