AVFoundation, cut edges in the preview layer

I am developing some kind of iOS app where I need to do some camera scan. This is my first experience with AVFoundation, I previously developed camera applications with the UIImagePickerController, but AVFoundation seems to be more powerful.

The problem is that it cuts off the edges in the preview level, regardless of the fact that I set the preview level frame to be larger than the preview controller.

This is my code:

AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
    AVCaptureDevice *photoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error = nil;
    AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:photoCaptureDevice error:&error];
    if(videoInput){
        [captureSession addInput:videoInput];
        [captureSession startRunning];
        NSLog(@"ok");
    }   
    AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
    previewLayer.frame = self.view.bounds;
    NSLog(@"%f %f", previewLayer.frame.size.width, previewLayer.frame.size.height);
    [self.view.layer addSublayer:previewLayer];

It would be very grateful for the help, Artem

+5
source share
2 answers

, , , . - , . , , , UIImagePickerController.

,

  previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
+20

, Rhythmic Fistman , AVCaptureVideoPreviewLayer - videoGravity. , :

previewLayer.frame = self.view.bounds;
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
// Specifies that the player should preserve the video’s aspect ratio and fill the layer’s bounds.

previewLayer.videoGravity = AVLayerVideoGravityResize; 
// Specifies that the video should be stretched to fill the layer’s bounds.
+7

All Articles