3

我在 iPhone 屏幕的一半上添加了相机预览。

-(void) viewDidAppear:(BOOL)animated
{
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    session.sessionPreset = AVCaptureSessionPresetMedium;

    CALayer *viewLayer = self.vImagePreview.view.layer;
    NSLog(@"viewLayer = %@", viewLayer);

    AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

    captureVideoPreviewLayer.frame = self.vImagePreview.view.bounds;
    [self.vImagePreview.view.layer addSublayer:captureVideoPreviewLayer];

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if (!input) {
        // Handle the error appropriately.
        NSLog(@"ERROR: trying to open camera: %@", error);
    }
    [session addInput:input];

    [session startRunning];
}

我在 viewDidLoad 方法中添加了相机视图控制器并设置了它的框架:

vImagePreview.view.frame = CGRectMake(0, 44, 320, 179);

但是相机视图不是320。知道如何设置相机视图的宽度添加这种方式吗?
高度很好,但宽度约为 100 宽。
就像在这个链接上宽度是 320 但相机视图由于某种原因更小(宽度)。![在此处输入图像描述][1]

4

2 回答 2

11
 captureVideoPreviewLayer.frame = self.vImagePreview.view.bounds;

这是罪魁祸首。

您需要添加videoGravity(指示视频如何在播放器层的边界矩形内显示。)AVLayerVideoGravityResizeAspectFill属性,然后分配bounds最后但并非最不重要的,您需要给出x and y- coordinates that establishes the center of a rectangle.

CGRect bounds=self.vImagePreview.view.layer.bounds;
avLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
avLayer.bounds=bounds;
avLayer.position=CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
于 2012-09-30T13:51:34.193 回答
0

更改您的相机预览屏幕宽度并检查。

于 2012-09-28T13:25:08.330 回答