【问题标题】:Camera preview in another view issue with width另一个视图中的相机预览宽度问题
【发布时间】:2012-09-28 09:52:45
【问题描述】:

我在 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]

【问题讨论】:

    标签: iphone objective-c ios ios6


    【解决方案1】:
     captureVideoPreviewLayer.frame = self.vImagePreview.view.bounds;
    

    这是罪魁祸首。

    您需要使用AVLayerVideoGravityResizeAspectFill 属性添加videoGravity(指示视频如何在播放器层的边界矩形内显示。) 然后分配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));
    

    【讨论】:

    • 这在我第一次设置时对我有用。但是在应用程序进入后台并且我再次执行此操作后,它被抵消了。
    【解决方案2】:

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

    【讨论】:

    • 预览屏幕为 320/179,如代码 captureVideoPreviewLayer.frame = self.vImagePreview.view.bounds;我也将这个值 320/179 添加到 previewLayer。
    猜你喜欢
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多