AVFoundation,在预览层中切掉边缘 [英] AVFoundation, cut off edges in the preview layer

查看:43
本文介绍了AVFoundation,在预览层中切掉边缘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一些iOS应用程序,需要在其中进行一些相机扫描.这是我第一次接触AVFoundation,之前我曾使用UIImagePickerController开发相机应用程序,但AVFoundation似乎更强大.

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

问题在于,无论我将预览层的帧设置为与视图控制器的帧一样大,它都会切断预览层的边缘.

The problem is that it cuts off edges in the preview layer, regardless of that I set preview layer's frame as big as view controller's one.

这是我的代码:

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];

非常感谢您的帮助,阿尔特姆(Artem)

Would be very thankful for a help, Artem

推荐答案

这是因为要捕获的视频的纵横比与屏幕的纵横比不同.您无能为力-它是字母装箱或图像的非均匀缩放.您可以 在短边上丢掉一些像素,这可能就是UIImagePickerController所做的.

This is because the aspect ratio of the video that you're capturing is different to that of the screen. There's not much you can do - it's either letter boxing or non uniform scaling of the image. You can throw away some pixels on the short edge, which is probably what UIImagePickerController does.

如果您想要这种行为,请尝试设置

If you want this behaviour, try setting

  previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

这篇关于AVFoundation,在预览层中切掉边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆