ioscocoa-touchavcam

AVCamPreviewView initialization is slow and sometimes causes crash after closing / reopening view - using AVCam example project


I'm using the AVCam example project, the latest version from the start of this month (Feb 2014). I have added flash selection functionality and removed the recording feature but I don't think that has any relevance to the issue.

When changing view, and reopening the AVCam view a number of times in succession the app either crashes, or it takes a long time for the preview view to initialize (~15 seconds). This only occurs sometimes.

I assumed the issue relates to clean up when changing view, but the example has what looks to be thorough clean up:

- (void)viewDidDisappear:(BOOL)animated
{
dispatch_async([self sessionQueue], ^{
    [[self session] stopRunning];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:AVCaptureDeviceSubjectAreaDidChangeNotification object:[[self videoDeviceInput] device]];
        [[NSNotificationCenter defaultCenter] removeObserver:[self runtimeErrorHandlingObserver]];

        [self removeObserver:self forKeyPath:@"sessionRunningAndDeviceAuthorized" context:SessionRunningAndDeviceAuthorizedContext];
        [self removeObserver:self forKeyPath:@"stillImageOutput.capturingStillImage" context:CapturingStillImageContext];
        [self removeObserver:self forKeyPath:@"movieFileOutput.recording" context:RecordingContext];
    });
}

Here is my code for changing the view (sending captured image in at the same time) (I'm loading the view using a storyboard modal action):

-(void)dealWithNewImage:(UIImage*)imageIn {

    [self saveCamState];

    //change view and send us the image
    UIStoryboard *storyboard = self.storyboard;
    DrawingController *drawView = (DrawingController *)[storyboard instantiateViewControllerWithIdentifier:@"DrawingView"];

    drawView.imageIn = imageIn;
    [self presentViewController:drawView animated:NO completion:nil];

}

I have no idea what is causing the crash and why sometimes the camera preview takes ~15 seconds to display.

Thanks in advance for any help!


Solution

  • Adding the following, within the viewDidDisappear method, after [[self session] stopRunning]; fixed the issue for me:

    for(AVCaptureInput *input in captureSession.inputs) {
        [captureSession removeInput:input];
    }
    
    for(AVCaptureOutput *output in captureSession.outputs) {
        [captureSession removeOutput:output];
    }