在自定义框架中显示相机
Displaying camera in custom frame
我正在尝试将相机加载到自定义 CGRect
中,但我无法这样做,因为视图似乎受相机方面的限制。这是我正在使用的代码:
AVCaptureSession *session = [[AVCaptureSession alloc] init];
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (videoDevice)
{
NSError *error;
AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
if (!error)
{
if ([session canAddInput:videoInput])
{
[session addInput:videoInput];
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
previewLayer.frame = self.view.bounds;
CGFloat x = self.view.bounds.origin.x;
CGFloat y = self.view.bounds.origin.y;
CGFloat width = self.view.bounds.size.width;
CGFloat height = 206;
CGRect newFrame = CGRectMake(x, y, width, height);
previewLayer.frame = newFrame;
[self.view.layer addSublayer:previewLayer];
[session startRunning];
}
}
}
这是应用程序当前显示的框架:
但是我需要这样装帧:
我不知道如何 "unlock" 相机框架或调整纵横比。是否可以在不降低图像质量的情况下获得我想要的结果,如果可以,如何实现?
在代码末尾添加这一行:
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
我正在尝试将相机加载到自定义 CGRect
中,但我无法这样做,因为视图似乎受相机方面的限制。这是我正在使用的代码:
AVCaptureSession *session = [[AVCaptureSession alloc] init];
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (videoDevice)
{
NSError *error;
AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
if (!error)
{
if ([session canAddInput:videoInput])
{
[session addInput:videoInput];
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
previewLayer.frame = self.view.bounds;
CGFloat x = self.view.bounds.origin.x;
CGFloat y = self.view.bounds.origin.y;
CGFloat width = self.view.bounds.size.width;
CGFloat height = 206;
CGRect newFrame = CGRectMake(x, y, width, height);
previewLayer.frame = newFrame;
[self.view.layer addSublayer:previewLayer];
[session startRunning];
}
}
}
这是应用程序当前显示的框架:
但是我需要这样装帧:
我不知道如何 "unlock" 相机框架或调整纵横比。是否可以在不降低图像质量的情况下获得我想要的结果,如果可以,如何实现?
在代码末尾添加这一行:
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;