除了Swift之外,我基本上都是在回答这个问题AVCaptureVideoPreviewLayer orientation - need landscape之后。
我的目标是iOS 8,而AVCaptureVideoPreviewLayer似乎没有setVideoOrientation功能。
我应该如何检测方向已更改,并正确旋转AVCaptureVideoPreviewLayer?
答案 0 :(得分:19)
在viewWillLayoutSubviews
检测并应用它。
以下是:
override func viewWillLayoutSubviews() {
let orientation: UIDeviceOrientation = UIDevice.currentDevice().orientation
print(orientation)
switch (orientation) {
case .Portrait:
previewLayer?.connection.videoOrientation = .Portrait
case .LandscapeRight:
previewLayer?.connection.videoOrientation = .LandscapeLeft
case .LandscapeLeft:
previewLayer?.connection.videoOrientation = .LandscapeRight
default:
previewLayer?.connection.videoOrientation = .Portrait
}
}
答案 1 :(得分:0)
更新Swift 3
override func viewWillLayoutSubviews() {
let orientation: UIDeviceOrientation = UIDevice.current.orientation
print(orientation)
switch (orientation) {
case .portrait:
videoPreviewLayer?.connection.videoOrientation = .portrait
case .landscapeRight:
videoPreviewLayer?.connection.videoOrientation = .landscapeLeft
case .landscapeLeft:
videoPreviewLayer?.connection.videoOrientation = .landscapeRight
case .portraitUpsideDown:
videoPreviewLayer?.connection.videoOrientation = .portraitUpsideDown
default:
videoPreviewLayer?.connection.videoOrientation = .portraitUpsideDown
}
}
答案 2 :(得分:0)
对于我来说,当我左右旋转iPad时,我需要它来改变方向,对我有帮助的解决方案是:
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<input type="text" data-bind="textInput: value" />
答案 3 :(得分:0)
ViewController中的Xamarin解决方案;
public override void ViewWillLayoutSubviews()
{
base.ViewWillLayoutSubviews();
var videoPreviewLayerConnection = PreviewView.VideoPreviewLayer.Connection;
if (videoPreviewLayerConnection != null)
{
var deviceOrientation = UIDevice.CurrentDevice.Orientation;
if (!deviceOrientation.IsPortrait() && !deviceOrientation.IsLandscape())
return;
var newVideoOrientation = VideoOrientationFor(deviceOrientation);
var oldSize = View.Frame.Size;
var oldVideoOrientation = videoPreviewLayerConnection.VideoOrientation;
videoPreviewLayerConnection.VideoOrientation = newVideoOrientation;
}
}
答案 4 :(得分:0)
Swift 4中的另一个解决方案是使用Notification。
NotificationCenter.default.addObserver(self,
selector: #selector(detected),
name:
UIDevice.orientationDidChangeNotification, 对象:无)
在选择器功能中,您可以检查方向:
let orientation: UIDeviceOrientation = UIDevice.current.orientation
switch orientation {
case .portrait:
stuff()
case .landscapeLeft:
stuffOther()
case .landscapeRight
stuffOther()
default:
stuffOther()
}
请记住在不需要时删除观察者。
非常重要:您的物理iPhone不能锁定旋转(我为此损失了2个小时,因为在这种情况下,该方法未被拦截:))。