Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 锁定方向的视图大小_Ios_Swift3_Uideviceorientation - Fatal编程技术网

Ios 锁定方向的视图大小

Ios 锁定方向的视图大小,ios,swift3,uideviceorientation,Ios,Swift3,Uideviceorientation,即使用户锁定了屏幕方向,是否有办法检测屏幕方向?我的应用程序使用自定义相机视图,我希望能够根据方向以横向或纵向方式保存图像。我目前正在使用下面的代码来检测方向,但是这只有在用户没有锁定的情况下才有效 override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { let orientation: UIDeviceOrientat

即使用户锁定了屏幕方向,是否有办法检测屏幕方向?我的应用程序使用自定义相机视图,我希望能够根据方向以横向或纵向方式保存图像。我目前正在使用下面的代码来检测方向,但是这只有在用户没有锁定的情况下才有效

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        let orientation: UIDeviceOrientation = UIDevice.current.orientation

        switch (orientation) {
        case .portrait:
            print("Portrait")
        case .landscapeRight:
            print("Left")
        case .landscapeLeft:
            print("Right")
        default:break
        }
    }

如果这不是正确的工作方式与相机方向,请指导我在正确的方向

我认为唯一的方法是在项目中同时启用纵向和横向模式:

然后,在所有不需要横向模式的视图控制器中重写这些属性

override var shouldAutorotate: Bool {
    return false
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return .portrait
}
在带有嵌入式摄影机的视图控制器中,刚设置的shouldAutorotate为false,并添加在横向中使用的额外支持的界面方向


请注意,如果使用UINavigationController或uitabarcontroller,它将无法工作,因此您应该创建以下扩展:

您的视图控制器中是否嵌入了自定义相机?如果是这样,我知道现在您只能以肖像模式保存图像?是的,我需要能够一次拍摄多张图像,并将所有图像一次性保存到我的个人资料中。所以苹果普通的图像采集器对我来说不起作用。一切正常,除了现在所有的图像都是肖像。难道绝对没有办法在景观中获得图像吗?这似乎有点奇怪这是一个黑客,但你可能可以利用加速计和/或陀螺仪来知道设备何时改变了它的功能orientation@Malik,我该怎么做呢?你必须在这两方面都读一读。这里有一个链接来解释它的基本原理,我想我们互相误解了。我可能解释错了。如果我在拍照后硬编码UIImageOrientation,我可以将图像保存为横向或纵向。但是,我想在拍照时检测设备是否处于横向或纵向模式,以便知道如何旋转图像。这是可行的,但如果用户在设备上锁定了方向,则没有效果。