Setting a background image/view to live camera view?

前端 未结 3 812
失恋的感觉
失恋的感觉 2020-12-17 03:01

Is it possible to set a background for a particular view controller to show a live camera view? If so, could one lead me in the right direction to make this possible?

3条回答
  •  清歌不尽
    2020-12-17 03:40

    Import:

    #import 
    

    To add camera view to a controller's view add this code in the viewDidLoad:

    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    session.sessionPreset = AVCaptureSessionPresetHigh;
    
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    [session addInput:input];
    
    AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    newCaptureVideoPreviewLayer.frame = self.view.bounds;
    
    [self.view.layer addSublayer:newCaptureVideoPreviewLayer];
    
    [session startRunning];
    

提交回复
热议问题