OGeek|极客世界-中国程序员成长平台

标题: ios - 相机预览不填满屏幕 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 16:44
标题: ios - 相机预览不填满屏幕

这就是我在 iPad3 中以横向模式设置相机预览的方式

- (void)viewDidAppearBOOL)animated

{

AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPreset640x480;

CALayer *viewLayer = self.vImagePreview.layer;

NSLog(@"viewLayer = %@", viewLayer);

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

captureVideoPreviewLayer.frame = self.vImagePreview.bounds;
[self.vImagePreview.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];

stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[stillImageOutput setOutputSettingsutputSettings];

[session addOutput:stillImageOutput];

[session startRunning];

}

在 Storyboard 中,我将 View 设置为填满整个屏幕,但它最终看起来像这样:

enter image description here

此外,图像也旋转了 90 度。如何更改此设置,以便让相机预览充满整个屏幕?

谢谢。



Best Answer-推荐答案


旋转图层:

    switch (self.interfaceOrientation)
    {
     case UIInterfaceOrientationLandscapeRight:
       [captureVideoPreviewLayer setAffineTransform:CGAffineTransformMakeRotation(-M_PI / 2)];
       break;
     case UIInterfaceOrientationLandscapeLeft:
       [captureVideoPreviewLayer setAffineTransform:CGAffineTransformMakeRotation(M_PI / 2)];
       break;
    }

[self.vImagePreview.layer addSublayer:captureVideoPreviewLayer]; 

关于ios - 相机预览不填满屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12706851/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4