AVCapture不会填满整个视图

时间:2015-05-01 02:58:23

标签: ios objective-c ios-autolayout

我正在尝试在视图中显示实时摄像机视频,但是当我运行以下代码时,它不会填满整个屏幕。它只能填充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正在运行,但运行时则不行。

1 个答案:

答案 0 :(得分:0)

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

中视图的边界