Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reopening AVCaptureSession

I have an application which takes some pictures. My whole application is based on the AVCam sample code from WWDC 2010. I've messed with it a lot and yet, up until now I can't figure out how to release the camera view properly which releases the camera session...

All i'm trying to do is the following:

  • Open camera view controller
  • Take some photos
  • Close camera view Controller
  • Open it again

The second time I push the viewController the session is lost, preview is not available and capturing is not available as well. I've published full example code on github.

My workaround for the issue was not to release the camera at all so the Camera View Controller acts as a Singleton, which I think is not the right way. moreover, with this behavior I couldn't figure out a way to support camera when application went to the background (phone call for example).

Please advice. How do I destruct the camera session? and is it important to do so?

like image 203
EladG Avatar asked Oct 13 '11 10:10

EladG


1 Answers

I've added the following message to AVCamCaptureManager

- (void) destroySession {

    if ([delegate respondsToSelector:@selector(captureManagerSessionWillEnd:)]) {
        [delegate captureManagerSessionWillEnd:self];
    }

    // remove the device inputs
    [session removeInput:[self videoInput]];
    [session removeInput:[self audioInput]];

    // release
    [session release];

    // remove AVCamRecorder
    [recorder release];

    if ([delegate respondsToSelector:@selector(captureManagerSessionEnded:)]) {
        [delegate captureManagerSessionEnded:self];
    }
}

I'm calling destroySession when the viewController holding the camera get close (on my example it's -closeCamera: of AVCamViewController).

For the full working example, you're welcome to download AVCam-CameraReleaseTest on github.com

like image 100
EladG Avatar answered Nov 03 '22 03:11

EladG