Set custom camera view in objective C

#import <AVFoundation/AVFoundation.h>

Following variable define in .h file

@property(nonatomic, retain) AVCaptureStillImageOutput *stillImageOutput;
AVCaptureDevice *device;
AVCaptureDeviceInput *input;
IBOutlet UIView *viewCamera;
IBOutlet UIButton *btnCameraMode;

 

Following Method implemented in .m file for display custom camera preview in your screen


-(void)setCameraPreview{
    
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    session.sessionPreset = AVCaptureSessionPresetPhoto;
    
    AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    captureVideoPreviewLayer.frame = viewCamera.bounds;
    [captureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    [viewCamera.layer addSublayer:captureVideoPreviewLayer];
    if (btnCameraMode.isSelected == false) {
        device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    }else{
        NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
        for (AVCaptureDevice *device1 in videoDevices) {
            if (device1.position == AVCaptureDevicePositionFront) {
                device = device1;
                break;
            }
        }
    }
    
    NSError *error = nil;
    input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if (input == nil) {
        NSLog(@"ERROR: trying to open camera: %@", error);
    }
    [session addInput:input];
    [session startRunning];
    
    _stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
    [_stillImageOutput setOutputSettings:outputSettings];
    
    [session addOutput:_stillImageOutput];
    [viewCamera addSubview:btnFlsh];
    [viewCamera addSubview:btnCameraMode];
    if (btnFlsh.isSelected == true) {
        [self flashOn:device];
    }else{
        [self flashOff:device];
    }
}

Set flash on/off in camera

-(void)flashOn:(AVCaptureDevice*)deviceFlash
{
    BOOL success = [deviceFlash lockForConfiguration:Nil];
    if (success && btnCameraMode.isSelected == false) {
        deviceFlash.flashMode = AVCaptureFlashModeOn;
        [deviceFlash unlockForConfiguration];
    }
}

-(void)flashOff:(AVCaptureDevice*)deviceFlash
{
    BOOL success = [deviceFlash lockForConfiguration:Nil];
    if (success && btnCameraMode.isSelected == false) {
        deviceFlash.flashMode = AVCaptureFlashModeOff;
        [deviceFlash unlockForConfiguration];
    }
}

-(IBAction)btnCameraFlash_click:(id)sender
{
    if (btnFlsh.isSelected == true) {
        btnFlsh.selected = false;
        [self flashOff:device];
    }else{
        btnFlsh.selected = true;
        [self flashOn:device];
    }
}

set camera mode

-(IBAction)btnCameraMode_click:(id)sender
{
    if (btnCameraMode.isSelected == true) {
        btnCameraMode.selected = false;
        [self setCameraPreview];
    }else{
        btnCameraMode.selected = true;
        [self setCameraPreview];
    }
}

Tack picture or capture camera picture

- (IBAction)btnTakePicture_click:(UIButton *)sender {
    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in _stillImageOutput.connections)
    {
        for (AVCaptureInputPort *port in [connection inputPorts])
        {
            if ([[port mediaType] isEqual:AVMediaTypeVideo] )
            {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection) { break; }
    }
    
    NSLog(@"about to request a capture from: %@", _stillImageOutput);
    [_stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
     {
         NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
         UIImage *image = [[UIImage alloc] initWithData:imageData];
         if (btnCameraMode.isSelected == true){
             image = [UIImage imageWithCGImage:[image CGImage] scale:[image scale] orientation: UIImageOrientationLeftMirrored];
         }
     }];
}