【问题标题】:Displaying camera in custom frame在自定义框架中显示相机
【发布时间】:2016-01-31 22:45:49
【问题描述】:

我正在尝试将摄像头加载到自定义 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];
        }
    }
}

这是应用当前显示的框架:

但我需要这样装帧:

我不知道如何“解锁”相机框架或调整纵横比。在不降低图像质量的情况下是否可以实现我想要的结果?如果可以,如何实现?

【问题讨论】:

  • 问题解决了吗?
  • @metronic 嘿,谢谢你的回答。我还没能做到——昨晚有点晚了,我在学校还没有带闪电电缆。我会尽快回到家,大约 2-3 小时后。我会在尝试后立即报告
  • @metronic 是的,解决了它!谢谢!

标签: ios objective-c camera avfoundation


【解决方案1】:

在代码末尾添加这一行:

previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多