Capturing video in full screen with AVCaptureSession

I need my app to show a preview of what the front camera is doing, and the dimensions of this video stream must fit the screen size of the device.

But there are black bars appearing at the top and bottom of the preview screen ... enter image description here

How can I capture and view a full screen video? Note. I don't want to resize the video!

Below is my code:

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

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];

[session addInput:input];

UIView *cameraPreviewView = [[UIView alloc] initWithFrame:self.view.frame] ;
[self.view addSubview:cameraPreviewView];

AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[session addOutput:output];

dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
[output setSampleBufferDelegate:self queue:queue];

AVCaptureVideoPreviewLayer *cameraPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
cameraPreviewLayer.frame = cameraPreviewView.bounds;

[cameraPreviewView.layer addSublayer:cameraPreviewLayer];
[cameraPreviewLayer setBackgroundColor:[[UIColor blackColor] CGColor]];
[cameraPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspect];

[session startRunning];

      

+3


source to share





All Articles