5

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?

EladG
  • 794
  • 9
  • 21

2 Answers2

5

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

EladG
  • 794
  • 9
  • 21
  • helped a lot thx. If you use AVCam as a ModalViewController you can just call [captureManager destroySession] in the dealloc Method. – lukaskrieger Apr 17 '13 at 13:32
0

G

I think that may help you have a look .

http://red-glasses.com/index.php/tutorials/ios4-take-photos-with-live-video-preview-using-avfoundation/

B25Dec
  • 2,301
  • 5
  • 31
  • 54
  • I already have an application that captures photos and save it to the library... As I wrote, problem is, how do I close the session properly so it can be reopen??? – EladG Oct 13 '11 at 15:37
  • 2
    i also faced this problem but where i got the solution by doing this thing [session removeInput:input]; input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:nil]; [session addInput:input]; i removed the previous input and then add the new one it helps me to alloc each time a new input and i able to click each time a new picture with opening and closing the camera and i have done it in a view not in a singleton class and one more thing i am not present modelling my camera view i am just adding it i hope it may help you – B25Dec Oct 14 '11 at 05:29
  • Thanks Ballu!! your comment help me a lot to fix the issue! I will post the full answer as soon as I fix some display issue with my "VideoPreviewView". – EladG Oct 16 '11 at 11:47
  • Thanks for your answer. It really does the job, BUT it also makes some weird thing: I call it on the controller dealloc method. About a second after closing the controller (probably the dealloc get called...) I get for about 0.5 a red effect on the top bar like there was some crash. Any idea why it happens? – bashan Jan 04 '14 at 08:48
  • @bashan Just call it on ViewWillDisappear and call the restore session on ViewWillAppear – Coldsteel48 Oct 19 '16 at 15:37