2012-10-03 182 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