2

I'm trying to make a camera overlay view on top of the camera accomodate according to the screen orientation. Since even if you lock your screen orientation on the settings, the camera still rotates its UI (the flash and camera buttons that are on the live preview), if I don't move the elements I have on the overlay view, it stays on top of those.

I already accomplished this by registering to the UIDeviceOrientationDidChangeNotification notification, and then reading the value on [UIDevice currentDevice].orientation and setting the transform value of my camera overlay view.

The problem with this is the UIDeviceOrientationDidChangeNotification notification doesn't fire when the user locks the interface on their iPhone (but the camera keeps rotating!).

I've tried a number of alternatives, like registering to the PLCameraDeviceOrientationChangedNotification notification that I found being fired when registering myself as an observer for every notification, but it doesn't contain the orientation on the userInfo dictionary. I also tried setting up a NSTimer to fire every 0.5s and check the [UIDevice currentDevice].orientation, but it always reports UIDeviceOrientationPortrait when it's locked, regardless of the actual device orientation.

Is there any way I can find out the orientation the camera is in, and ideally also be notified when it changes even with the interface orientation locked by the user?

Thanks.

Javier Soto
  • 4,840
  • 4
  • 26
  • 46

1 Answers1

1

My opinion, it is impossible to get device messages about orientation if user locked it device orientation.

But the accelerometer never lies and should give you the right information.

Juste take a look at : Detect iPhone screen orientation

EDIT : here's the apple's sample, for using accelerometer.

https://developer.apple.com/library/ios/#samplecode/AccelerometerGraph/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007410

Community
  • 1
  • 1
Martin
  • 11,881
  • 6
  • 64
  • 110
  • Do you think that's what the camera does? And also.. is it easy to get a UIDeciveOrientation value from the acceleration...? – Javier Soto Feb 20 '12 at 16:16
  • I think when you lock your screen orientation, the system prevent any viewController to call the autorotate message. If so, you have to use something different than UIDeviceOrientation, like the accelerometer. Just try, it not very difficult if you find a good sample (apple one in my edit) – Martin Feb 20 '12 at 16:21
  • Mmmm yea, that's just an example to use the accelerometer, but it's not so easy to get the UIDeviceOrientation from that, and I wouldn't want to "figure out a way" myself that then doesn't report the same orientation the camera believes the device is in... Thanks though! – Javier Soto Feb 20 '12 at 16:58
  • You can't get the UIDeviceOrientation that way, but some coordinates that tell how many degrees your phone is inclined. Ok, i'm going to search in my cocoa projects and come back with an example – Martin Feb 21 '12 at 11:18
  • But I would need to have the same value for the orientation that the camera thinks the device is in. Otherwise, my adjustments to my camera overlay UI would differ from those of the camera controls.... That's why taking the degrees and then assuming an orientation is dangerous imo. But I'm afraid there wouldn't be another way :( – Javier Soto Feb 21 '12 at 12:51
  • Yes, i'm pretty sure (around 99%) that UIDeviceOrientation stays to Portrait when user locks the phone. – Martin Feb 21 '12 at 15:39
  • I mean, as you said, that [UIDevice currentDevice].orientation always return UIDeviceOrientationPortrait when the user locks the phone. – Martin Feb 22 '12 at 15:51