0

Is there any way to detect the screen orientation even when the user has locked his orientation? My App uses a custom camera view and I would like to be able to save images in either landscape or portrait depending on the orientation. I am currently using the following code to detect the orientation, however this only works if the user has not locked it.

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
        }
    }

If this is not the correct way to work with the camera orientation, please guide me in the right direction.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Christopher Smit
  • 953
  • 11
  • 27
  • Do you have your custom camera embedded in your view controller? If so, I understand that now you're able to save images only in a portrait mode? – kamil3 Jul 19 '17 at 08:58
  • Yes I do, I required to be able to take more than one image at a time and save all images to my profile all at once. So apple's normal imagepicker did not work for me. Everything works, except now all images are in portrait. Is there absolutely NO WAY to get the images in landscape? This seems a bit odd – Christopher Smit Jul 19 '17 at 09:01
  • It's a hack but you can probably exploit the accelerometer and/or gyroscope to know when the device has changed it's orientation – Malik Jul 19 '17 at 09:07
  • @Malik, How can I do that? – Christopher Smit Jul 19 '17 at 09:09
  • You'll have to do a little bit of reading on both. Here is a link to explain the basics of it http://nshipster.com/cmdevicemotion/ – Malik Jul 19 '17 at 09:14

1 Answers1

0

I believe that the only way to do this is enabling both portrait and landscape mode in project: enter image description here

Then, override these properties in all your view controllers that don't need a landscape mode.

override var shouldAutorotate: Bool {
    return false
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return .portrait
}

In the view controller with embedded camera just set shouldAutorotate false and add extra supportedInterfaceOrientations that are used in landscape.

Note that if you use UINavigationController or UITabBarController it will not work, so you should create these extensions: https://stackoverflow.com/a/39674618/5381663

kamil3
  • 1,232
  • 1
  • 14
  • 19
  • I think we misunderstood each other. I might have explained wrong. I am able to save the image as landscape or portrait if I hardcode the UIImageOrientation after the picture is taken. However, I want to detect if the device was in landscape or portrait mode when taking the picture so I know how to rotate the image. This works, but if the user has his orientation locked on his device, then it has no effect. – Christopher Smit Jul 19 '17 at 09:24