iOS taking photo programmatically

前端 未结 5 2025
执笔经年
执笔经年 2020-11-28 07:44

I know this is possible, saw this in some apps (iGotYa is I believe the most famous). I know how to set up everything for taking photos, saving it and everything. But how ca

5条回答
  •  [愿得一人]
    2020-11-28 08:24

    Here is the code for Objective -C Custom Camera. You can add features, buttons according to your wish.

    #import "CustomCameraVC.h"
    
    @interface CustomCameraVC ()  {
        BOOL frontCamera;
    }
    @property (strong,nonatomic) AVCaptureSession *captureSession;
    @property (strong,nonatomic) AVCaptureStillImageOutput *stillImageOutput;
    @property (strong,nonatomic) AVCaptureVideoPreviewLayer *videoPreviewLayer;
    @property (weak, nonatomic) IBOutlet UIView *viewCamera;
    
    
    @end
    
    @implementation CustomCameraVC
    
    - (void)viewDidLoad {
        [super viewDidLoad];        
    }
    
    -(void)viewWillAppear:(BOOL)animated  {
        [super viewWillAppear:YES];
        frontCamera = NO;
        [self showCameraWithFrontCamera:frontCamera];
    
    }
    
    -(void)showCameraWithFrontCamera:(BOOL)flag {
        self.captureSession = [[AVCaptureSession alloc]init];
        self.captureSession.sessionPreset = AVCaptureSessionPresetPhoto;
        AVCaptureDevice *captureDevice;
        if(flag)  {
          captureDevice= [self frontCamera];
        }
        else {
          captureDevice= [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        }
        NSError *error = nil;
        AVCaptureDeviceInput *input =   [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
    
        [self.captureSession addInput:input];
        self.stillImageOutput = [AVCaptureStillImageOutput new];
        self.stillImageOutput.outputSettings = @{AVVideoCodecKey:AVVideoCodecJPEG};
        [self.captureSession addOutput:_stillImageOutput];
        self.videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
    
        self.videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
        self.videoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationPortrait;
        [self.viewCamera.layer addSublayer:self.videoPreviewLayer];
        [self.captureSession startRunning];
        self.videoPreviewLayer.frame = self.viewCamera.bounds;
    }
    
    
    - (AVCaptureDevice *)frontCamera {
        NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
        for (AVCaptureDevice *device in devices) {
            if ([device position] == AVCaptureDevicePositionFront) {
                return device;
            }
        }
        return nil;
    }
    
    
    - (IBAction)btnCaptureImagePressed:(id)sender {
    
         AVCaptureConnection * videoConnection =  [_stillImageOutput connectionWithMediaType:AVMediaTypeVideo];
    
        [_stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef  _Nullable sampleBuffer, NSError * _Nullable error) {
           NSData *imageData =  [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:sampleBuffer];
            UIImage *image = [[UIImage alloc]initWithData: imageData];
            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
        }];
    
    }
    
    @end
    

提交回复
热议问题