相机预览不填满屏幕
Posted
技术标签:
【中文标题】相机预览不填满屏幕【英文标题】:Camera Preview does not fill screen 【发布时间】:2012-10-03 10:45:14 【问题描述】:这就是我在 iPad3 中以横向模式设置相机预览的方式
- (void)viewDidAppear:(BOOL)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 setOutputSettings:outputSettings];
[session addOutput:stillImageOutput];
[session startRunning];
在情节提要中,我将视图设置为填满整个屏幕,但它最终看起来像这样:
此外,图像也旋转了 90 度。如何更改此设置,以便让相机预览充满整个屏幕?
谢谢。
【问题讨论】:
【参考方案1】:旋转图层:
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];
【讨论】:
这确实有效。但是我最终使用了 captureVideoPreviewLayer.orientation = UIInterfaceOrientationLandscapeLeft;谢谢! 请注意 AVCaptureVideoPreviewLayer.orientation 被标记为已弃用以上是关于相机预览不填满屏幕的主要内容,如果未能解决你的问题,请参考以下文章
LinearLayout 和 CameraSource 不填满屏幕