相机预览不会填满屏幕

时间:2012-10-03 10:45:15

标签: ios ipad avfoundation ios6

这就是我在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];

}

在故事板中,我将视图设置为填满整个屏幕,但最终看起来像这样:

enter image description here

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

感谢。

1 个答案:

答案 0 :(得分:3)

旋转你的图层​​:

    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];