1

我正在尝试在视图中显示实时摄像头视频,但是当我运行以下代码时,它并没有填满整个屏幕。它仅填充 3.5 英寸屏幕尺寸(在 5.5 英寸屏幕上运行)。我已将autolayout每一侧的视图设置为 0。

    session = [[AVCaptureSession alloc] init];
    [session setSessionPreset:AVCaptureSessionPresetPhoto];
    AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error;
    AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];
    if ([session canAddInput:deviceInput]) {
        [session addInput:deviceInput];
    }
    AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    CALayer *rootLayer = [[self view] layer];
    [rootLayer setMasksToBounds:YES];
    CGRect frame = frameForCapture.frame;
    [previewLayer setFrame:frame];
    [rootLayer insertSublayer:previewLayer atIndex:0];
    stillImage = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil];
    [stillImage setOutputSettings:outputSettings];
    [session addOutput:stillImage];
    [session startRunning];

当我将视图控制器的尺寸从 3.5 英寸设置为 5.5 英寸并运行时,它会填满屏幕。当autolayout大小发生变化时它正在工作,但在它运行时不起作用。

4

1 回答 1

0

图层不像视图那样坚持自动调整大小/自动布局,您需要手动设置图层的框架调整大小。一个很好的方法是引入一个包含预览层的视图子类,并将其框架设置为视图的边界-layoutSubviews

于 2015-05-01T14:38:56.617 回答