Spi*_*ixx 5 xcode uiviewcontroller swift ios10 swift5
背景
我有一个使用AVFoundation的应用程序以拥有自定义相机。这发生在中OCRViewController。当我拍照时,会将捕获的图片发送到其他视图ImagePreviewViewController。
我使用Xcode 10.2.1 (10E1001)与Swift 5
目标
我想实现的是将的方向锁定ImagePreviewViewController为图像的原始方向。我已经知道如何获取图像的方向,但是我无法锁定视图的方向。
我得到这样的图像旋转: let imageOri = capturedImage?.imageOrientation
我尝试了什么?
我尝试了其他来源的公认答案:
在https://developer.apple.com/documentation/uikit/uiviewcontroller#//apple_ref/occ/instm/UIViewController/supportedInterfaceOrientations下的“ 处理视图旋转 ” 下阅读文档,内容如下:
在编写此查询时,我还尝试了许多建议的解决方案,但是,大多数方法似乎使用以下方法(或它的一种变体),它对我不起作用。
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
override func shouldAutorotate() -> Bool{
return false
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.Portrait
}
Run Code Online (Sandbox Code Playgroud)
从iOS 8开始,不推荐使用所有与旋转相关的方法。相反,旋转被视为视图控制器视图大小的变化,因此使用viewWillTransition(to:with :)方法进行报告。
但是,我不确定如何从这里开始。
有趣的代码片段
我的中使用了以下方法OCRViewController,在此实例化ImagePreviewViewController并附加捕获的图像。
func displayCapturedPhoto(capturedPhoto : UIImage) {
let imagePreviewViewController = storyboard?.instantiateViewController(withIdentifier: "ImagePreviewViewController") as! ImagePreviewViewController
imagePreviewViewController.capturedImage = capturedPhoto
navigationController?.pushViewController(imagePreviewViewController, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
ImagePreviewViewController我可以使用下面的覆盖函数来检测视图控制器的方向。
override func viewDidAppear(_ animated: Bool) {
if UIDevice.current.orientation.isLandscape {
print("Landscape")
} else {
print("Portrait")
}
}
Run Code Online (Sandbox Code Playgroud)
要限制一个屏幕的旋转,请使用它。
在应用程序委托中
var restrictRotation = Bool()
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if !restrictRotation {
return .portrait
} else {
return .all
}
}
Run Code Online (Sandbox Code Playgroud)
在您的视图控制器中添加该功能,
func restrictRotation(restrict : Bool) -> Void {
let appDelegate = UIApplication.shared.delegate as? AppDelegate
appDelegate?.restrictRotation = restrict
}
Run Code Online (Sandbox Code Playgroud)
在 ViewDidload() 方法中,调用该函数来禁用旋转。
self.restrictRotation(restrict: false)
Run Code Online (Sandbox Code Playgroud)
在 viewWillDisappear() 方法中,调用该函数启用旋转。
self.restrictRotation(restrict: true)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
98 次 |
| 最近记录: |