环境:Xcode 4,iOS 5,带有视网膜显示屏的iPod Touch第四代
我正在尝试编写一个简单的应用程序,它可以显示视频预览并拍摄照片。为了显示预览,我使用以下代码:
// Create the session
session = [[AVCaptureSession alloc] init];
// Set preset to the highest available
session.sessionPreset = AVCaptureSessionPresetPhoto;
// Give the frame for preview
CALayer *viewLayer = self.vPreview.layer;
NSLog(@"viewLayer = %@", viewLayer);
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer =
[[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
captureVideoPreviewLayer.frame = self.vPreview.bounds;
NSLog(@"Bounds: x:%d, y:%d,height:%d,width:%d",
self.vPreview.bounds.origin.x,self.vPreview.bounds.origin.y,
self.vPreview.bounds.size.height, self.vPreview.bounds.size.width);
[self.vPreview.layer addSublayer:captureVideoPreviewLayer];
// Get AV Device
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
// Add Input from the above device
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if ( !input ) NSLog(@"ERROR: trying to open camera: %@", error);
[session addInput:input];
我已经将vPreview连接到IB故事板上的UIView并且占据了整个屏幕。然而,该框架始终打印为0,0,0,0 (x,y,h,w)。有人可以告诉我出了什么问题吗?
还有几行代码设置了输出并运行会话。不过,此时框架已经错误了。有趣的是,我可以看到视频预览,但它是以纵向显示,而我的应用程序是全横向的。
CGRect
,可以使用这个方法NSLog(@"%@",NSStringFromCGRect(self.vPreview.bounds));
。 - NJones