- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
自 iOS 8.0 起,函数 didRotateFromInterfaceOrientation
已弃用,我现在不知道如何更改 AVCaptureVideoPreviewLayer 的方向适本地。
根据 "interfaceOrientation" is deprecated in iOS 8, How to change this method Objective C ,我有以下工作代码来获取基于 statusBarOrientation
的 AVCaptureVideoOrientation
:
- (AVCaptureVideoOrientation)getOrientation {
// Translate the orientation
switch ([[UIApplication sharedApplication] statusBarOrientation]) {
case UIInterfaceOrientationPortrait:
return AVCaptureVideoOrientationPortrait;
case UIInterfaceOrientationPortraitUpsideDown:
return AVCaptureVideoOrientationPortraitUpsideDown;
case UIInterfaceOrientationLandscapeLeft:
return AVCaptureVideoOrientationLandscapeLeft;
case UIInterfaceOrientationLandscapeRight:
return AVCaptureVideoOrientationLandscapeRight;
default:
return AVCaptureVideoOrientationPortrait;
}
}
当方向改变时,我需要调用以下函数:
[self.videoPreviewLayer.connection setVideoOrientation:[self getOrientation]];
但是我不知道放哪里合适。
由于 didRotateFromInterfaceOrientation
已被弃用并且 Apple 建议使用 viewWillTransitionToSize:withTransitionCoordinator:
,这似乎是很自然的尝试:
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
[self.videoPreviewLayer.connection setVideoOrientation:[self getOrientation]];
}];
}
但如果 UIViewController
正在显示,则不会调用此函数(似乎是基于 "viewWillTransitionToSize:" not called in iOS 9 when the view controller is presented modally 的已知错误)。即使解决了这个错误,还有另一个问题:方向更改不等同于大小更改。例如,如果 View 显示为 UIModalPresentationFormSheet
,如下所示,
SecondViewController *vc = [[SecondViewController alloc] init];
vc.view.backgroundColor = [UIColor greenColor];
vc.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:vc animated:true completion:nil];
在 iPad 的横向和纵向模式下呈现的 View 大小相同:
因此,我不能简单地使用 viewWillTransitionToSize
。
我注意到 UIDeviceOrientationDidChangeNotification
没有被弃用。伟大的!我尝试注册一个通知:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Some other code to set up the preview layer
// ...
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(deviceOrientationDidChange:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}
- (void)viewWillDisappear:(BOOL)animated
{
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)deviceOrientationDidChange:(NSNotification *)notification {
[self.videoPreviewLayer.connection setVideoOrientation:[self getOrientation]];
}
这几乎可以正常工作,而且已经关闭了!问题是,当我收到设备方向更改通知时,旋转 View 的动画可能会开始,也可能不会。或者换句话说,返回值 [[UIApplication sharedApplication] statusBarOrientation]
可能不正确,如果设备已经上下旋转但动画仍在挂起。
我不知道还能尝试什么 :( 有什么想法吗?
最佳答案
以下应该能够在 swift 2.1 中正确处理视频预览的方向:
override func viewWillAppear(animated: Bool) {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self,
selector: Selector("deviceOrientationDidChange:"),
name: UIDeviceOrientationDidChangeNotification,
object: nil)
}
override func viewDidDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self)
super.viewDidDisappear(animated)
}
func deviceOrientationDidChange(sender: AnyObject) {
let deviceOrientation = UIDevice.currentDevice().orientation;
switch (deviceOrientation)
{
case .Portrait:
previewLayer.connection.videoOrientation = .Portrait
case .LandscapeLeft:
previewLayer.connection.videoOrientation = .LandscapeRight
case .LandscapeRight:
previewLayer.connection.videoOrientation = .LandscapeLeft
case .PortraitUpsideDown:
previewLayer.connection.videoOrientation = .PortraitUpsideDown
default:
break
}
}
关于ios - didRotateFromInterfaceOrientation 在 iOS 8 中被弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34774658/
我是一名优秀的程序员,十分优秀!