0

Hello My App crash while I take photo using AVCaptureSession and go another view when I pop to my camera view app crash after 2-3 second.
I use this code for Open camera using AVCaptureSession.

session = [[AVCaptureSession alloc] init];
    session.sessionPreset = AVCaptureSessionPresetPhoto;

     captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    [captureVideoPreviewLayer setBackgroundColor:[[UIColor blackColor] CGColor]];
    [captureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    rootLayer = [_MainPreviewVIEW layer];

    [rootLayer addSublayer:captureVideoPreviewLayer];

    NSArray *devices = [AVCaptureDevice devices];

    for (AVCaptureDevice *device in devices)
    {
        NSLog(@"Device name: %@", [device localizedName]);

        if ([device hasMediaType:AVMediaTypeVideo])
        {
            if ([device position] == AVCaptureDevicePositionBack)
            {
                NSLog(@"Device position : back");
                backCamera = device;
                [device lockForConfiguration:nil];
                [device unlockForConfiguration];
            }
        }
    }

    NSError *error = nil;
    input = [AVCaptureDeviceInput deviceInputWithDevice:backCamera error:&error];
    if (!input)
    {
        NSLog(@"ERROR: trying to open camera: %@", error);
    }
    [session addInput:input];
    stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
    [stillImageOutput setOutputSettings:outputSettings];

    [session addOutput:stillImageOutput];

    [session startRunning];

I Got this error message.

[Viewcontroller respondsToSelector:]: message sent to deallocated instance 0x148286690**

I Use this method for take picture.

AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection1 in stillImageOutput.connections)
    {
        for (AVCaptureInputPort *port in [connection1 inputPorts])
        {
            if ([[port mediaType] isEqual:AVMediaTypeVideo] )
            {
                videoConnection = connection1;
                break;
            }
        }
        if (videoConnection)
        {
            break;
        }
    }
    NSLog(@"about to request a capture from: %@", stillImageOutput);
    [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
    {
        if (imageSampleBuffer != NULL)
        {
            NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
            Catchimg=[[UIImage alloc]initWithData:imageData];

            [session stopRunning];
        }
    }];

Then After I have use simple push view using navigation controller.
when I back to again this camera view it getting crash after 2-3 second.

Kaushik Movaliya
  • 799
  • 11
  • 27
  • Are you calling any method while doing transition? – Bharat Nakum Sep 30 '16 at 11:23
  • i just push view using navigation controller – Kaushik Movaliya Sep 30 '16 at 11:24
  • You are calling method after `viewcontroller` is deallocated. That's why you are facing this crash issue. – Bharat Nakum Sep 30 '16 at 11:40
  • Also, in `viewDidDisappear`, You should remove your `cameraSession` and set it to `nil`. – Bharat Nakum Sep 30 '16 at 11:42
  • @BharatNakum i call AVCaptureSession in view did load and image taken successfully save in UIImage, so are you suggest me how i can resolve it? – Kaushik Movaliya Sep 30 '16 at 11:44
  • i try it already but its not working perfect.. – Kaushik Movaliya Sep 30 '16 at 11:46
  • `Session` removed in `ViewDidDisappear`? If so, you've to again `initialize` it when `view controller` appears. – Bharat Nakum Sep 30 '16 at 11:49
  • Use delegate method of avcapturesession and put method of camera view in viewwillappear. In delegate method navigate with image. Check this http://stackoverflow.com/questions/8237471/proper-way-to-optimize-my-avcapturesession when u finished picking image this delegate method calls and in it u put your code of navigation with image. When u return viewwillappear method calls and again camera view is opening – Jitendra Modi Sep 30 '16 at 12:31
  • its not working perfect, i mean camera continuously start and i got this error " message sent to deallocated instance 0x18793db0" – Kaushik Movaliya Oct 06 '16 at 09:43

0 Answers0