看起来avcapturevideopreviewlayer的方向已在iOS 6中折旧。任何人都知道新代码?这是我目前的(折旧)代码:
[self setPreviewLayer:[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]]];
[[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
previewLayer.orientation = UIInterfaceOrientationLandscapeRight;
答案 0 :(得分:19)
您查看了文档吗?这只是一行:
图层的方向。 (在iOS 6.0中不推荐使用。videoOrientation (AVCaptureConnection)代替。)
所以使用:
[[AVCaptureVideoPreviewLayer connection] setVideoOrientation: AVCaptureVideoOrientationLandscapeRight];
或
AVCaptureVideoPreviewLayer.connection.videoOrientation= AVCaptureVideoOrientationLandscapeRight;
答案 1 :(得分:3)
我尝试使用videoOrientation(AVCaptureConnection)而不是弃用的方向(AVCaptureVideoPreviewLayer),但它不再旋转视频预览。
我取而代之:
AVCaptureVideoPreviewLayer *previewLayer = ...;
previewLayer.orientation = AVCaptureVideoOrientationLandscapeRight;
有了这个:
AVCaptureVideoPreviewLayer *previewLayer = ...;
previewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
但它没有旋转视频预览。问题是我在将AVCaptureDeviceInput添加到AVCaptureSession之前添加并修改了AVCaptureVideoPreviewLayer。因此,我的AVCaptureVideoPreviewLayer的连接为空。解决方案是在将AVCaptureDeviceInput添加到AVCaptureSession之后添加AVCaptureVideoPreviewLayer。
答案 2 :(得分:0)
就像@Nikolai发布的那样,请使用AVCaptureVideoPreviewLayer的连接videoOrientation
属性。
(之所以我再次发布这个原因是因为他的代码可能有点令人困惑,因为看起来connection
是一个类方法。希望这个例子说得清楚。)
替换以下内容:
AVCaptureVideoPreviewLayer *previewLayer = ...;
previewLayer.orientation = UIInterfaceOrientationLandscapeRight;
使用:
AVCaptureVideoPreviewLayer *previewLayer = ...;
previewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;