UIView中的相机预览未显示完整

时间:2014-12-31 10:26:30

标签: ios ios7 uiview camera avcapturedevice

我在UIView内有UIViewController,使用以下代码在预览模式下显示相机。显示相机预览,但其高度与UIView不同。 UIView有限制领先空间到超级视图 - 40,跟踪空间到超级视图 - 40,将中心X对准超级视图,底部空间到超级视图 - 40,顶级空间到超级视图 - 40.

你能帮我解决一下这个问题。

感谢。

- (void)startCameraPreview {

    AVCaptureDevice* camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if (camera == nil) {
        return;
    }

    captureSession = [[AVCaptureSession alloc] init];
    captureSession.sessionPreset = AVCaptureSessionPresetMedium;

    AVCaptureDeviceInput *newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:camera error:nil];
    [captureSession addInput:newVideoInput];

    captureLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:captureSession];
    captureLayer.frame = captureView.bounds;
    [captureLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    [captureView.layer addSublayer:captureLayer];

    UIView *view = [self captureView];
    CALayer *viewLayer = [view layer];
    [viewLayer setMasksToBounds:YES];

    CGRect bounds = [view bounds];
    [captureLayer setFrame:bounds];

    // Start the session. This is done asychronously since -startRunning doesn't return until the session is running.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [captureSession startRunning];
    });

}

0 个答案:

没有答案