2012-10-03 133 views
2

这是我如何在我的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º度。我怎样才能改变这个,所以我可以让相机预览填满整个屏幕?

谢谢。

回答

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

这indeeds工作。 HOwever我结束了使用captureVideoPreviewLayer.orientation = UIInterfaceOrientationLandscapeLeft; 谢谢! – MobileCushion

+0

请注意,AVCaptureVideoPreviewLayer.orientation被标记为已弃用 – CSmith