2

Im trying to detect orientation changes in a UIImagePickerController (it inherits from UINavigationController : UIViewController : UIResponder : NSObject) and I tried override the method - (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)interfaceOrientation in UIViewController but no success...

any tips?

Thanks in advance...

Paulo Ferreira
  • 423
  • 2
  • 8
  • 16

3 Answers3

4

Subclassing UIImagePickerController is not supported!

This class is intended to be used as-is and does not support subclassing.

Maybe you could register for UIDeviceOrientationDidChangeNotification from UIDevice and use this?

Adam Woś
  • 2,493
  • 20
  • 21
  • Be aware that device orientation changes contain two more constants than interface orientation (namely: face up and face down) - you have to remember the last orientation you had (portrait/landscape) and ignore the face up/down changes to know which interface orientation you're actually in. – Adam Woś Jan 13 '10 at 17:14
  • 1
    Unfortunately if the device's orientation is locked the notifications are no longer posted, but the camera interface is still being updated. Did you find a way around this? – Felix Lamouroux Jan 09 '12 at 18:25
  • 1
    A further issue is that the device orientation does not necessarily match the camera orientation. – Felix Lamouroux Jan 10 '12 at 10:44
2

This is too late to answer here, but I'm expanding @Adam Woś answer,

Before presenting UIImagePickerController,

UIImagePickerController *controllerObject = [[UIImagePickerController alloc] init];
...
...
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:)name:UIDeviceOrientationDidChangeNotification  object:nil];
...
...
[self presentViewController:controllerObject animated:YES completion:nil];

- (void)orientationChanged:(NSNotification *)notification{
    [self adjustViewsForOrientation:UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]];
}

- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation
{
    if(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight)
    {
        NSLog(@".....landscape.....");
    }
    else if(orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown)
    {
        NSLog(@".....portrait.....");
    }
}

When UIImagePickerController get dismiss don't forget,

[[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

Also note that, as per @FelixLam comment on @Adam Woś answer,

if device orientation is locked then notifications no longer posted.

To handle this situation, one need to implement CoreMotion (alternative UIAccelerometer iOS < 6.0) to detect that device orientation is locked or not. Here's the blog for UIAccelerometer http://blog.sallarp.com/iphone-accelerometer-device-orientation/ and this is for CoreMotion https://github.com/tastyone/MotionOrientation You'll have to kick some logic to check this.

Good luck!

Hemang
  • 26,840
  • 19
  • 119
  • 186
0

From the official UIImagePickerController documentation:

Important: The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified, with one exception. In iPhone OS 3.1 and later, you can assign a custom view to the cameraOverlayView property and use that view to present additional information or manage the interactions between the camera interface and your code.

Adrian Kosmaczewski
  • 7,956
  • 4
  • 33
  • 59