0

My requirement in the app is to capture an image without previewing the UIImagePickerController So I have used following code to capture an image without presenting the same.

- (void)clickImage
{
    AVCaptureDevice *rearCamera = [self checkIfRearCameraAvailable];

    if (rearCamera != nil)
    {
        photoSession = [[AVCaptureSession alloc] init];

        NSError *error;
        AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:rearCamera error:&error];

        if (!error && [photoSession canAddInput:input])
        {
            [photoSession addInput:input];

            AVCapturePhotoOutput *output = [[AVCapturePhotoOutput alloc] init];


            if ([photoSession canAddOutput:output])
            {
                [photoSession addOutput:output];

                AVCaptureConnection *videoConnection = nil;

                for (AVCaptureConnection *connection in output.connections)
                {
                    for (AVCaptureInputPort *port in [connection inputPorts])
                    {
                        if ([[port mediaType] isEqual:AVMediaTypeVideo])
                        {
                            videoConnection = connection;
                            break;
                        }
                    }
                    if (videoConnection)
                    {
                        break;
                    }
                }

                if (videoConnection)
                {
                    [photoSession startRunning];

                    [output capturePhotoWithSettings:[AVCapturePhotoSettings photoSettings] delegate:self];
                }
            }
        }
    }
}

- (AVCaptureDevice *)checkIfRearCameraAvailable
{
    AVCaptureDevice *rearCamera;

    AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession =
    [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera]
                                                           mediaType:AVMediaTypeVideo
                                                            position:AVCaptureDevicePositionBack];

    NSArray *allCameras = [captureDeviceDiscoverySession devices];


    for (int i = 0; i < allCameras.count; i++)
    {
        AVCaptureDevice *camera = [allCameras objectAtIndex:i];

        if (camera.position == AVCaptureDevicePositionBack)
        {
            rearCamera = camera;
        }
    }

    return rearCamera;
}

- (void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhotoSampleBuffer:(CMSampleBufferRef)photoSampleBuffer previewPhotoSampleBuffer:(CMSampleBufferRef)previewPhotoSampleBuffer resolvedSettings:(AVCaptureResolvedPhotoSettings *)resolvedSettings bracketSettings:(AVCaptureBracketedStillImageSettings *)bracketSettings error:(NSError *)error
{
    if (error)
    {
        NSLog(@"error : %@", error.localizedDescription);
    }

    if (photoSampleBuffer)
    {
        NSData *data = [AVCapturePhotoOutput JPEGPhotoDataRepresentationForJPEGSampleBuffer:photoSampleBuffer
                                                                   previewPhotoSampleBuffer:previewPhotoSampleBuffer];
        UIImage *image = [UIImage imageWithData:data];

        _imgView.image = image;
    }
}

I have used above code to capture an image But the output image is returning is looking like in Night mode or like Negative Image.

Would you please review code and correct me with this?

Bhargav Soni
  • 1,067
  • 1
  • 9
  • 22

1 Answers1

0

I have found following sample code from Apple Developer's site:

https://developer.apple.com/library/content/samplecode/AVCam/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010112

Above sample code helped to get proper image. I have added this answer for other's help as well.

Bhargav Soni
  • 1,067
  • 1
  • 9
  • 22