Image Picker Camera Controls - Auto Rotate in iOS7

I have a photo application that overlays a custom button on the camera (for taking pictures, turning the flash on / off, other ordinary things, etc.).

I want the control interface to support only portrait orientation (I'm talking only about the control button / interface, not the actual captured image), which worked fine until iOS 6.

However, upgrading to xCode 5.0 and upgrading iPad 3 to iOS 7 (GM Seed for the 3rd generation iPad), I found that the camera selection interface automatically rotates when the orientation changes. Surprisingly, I tested the same build on the iPhone 5 (updated to iOS 7), but the problem of automatic rotation did not appear.

[To be sure, I checked the same code again in iOS 6, and automatic rotation did not happen on either the iPhone or iPad].

Just to demonstrate how I process my image picker, here is a bit of code snippet:

UIImagePickerController *pickercustom = [[UIImagePickerController alloc] init]; pickercustom.sourceType = UIImagePickerControllerSourceTypeCamera; pickercustom.showsCameraControls = NO; pickercustom.wantsFullScreenLayout = YES; pickercustom.navigationBarHidden=YES; pickercustom.view.userInteractionEnabled=YES; if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { if (IPAD.userInterfaceIdiom == UIUserInterfaceIdiomPad) { pickercustom.delegate = self; UIDevice *currentDevice = [UIDevice currentDevice]; while ([currentDevice isGeneratingDeviceOrientationNotifications]) [currentDevice endGeneratingDeviceOrientationNotifications]; [self presentViewController:pickercustom animated:YES completion:nil]; while ([currentDevice isGeneratingDeviceOrientationNotifications]) [currentDevice endGeneratingDeviceOrientationNotifications]; } else { pickercustom.delegate = self; [self presentViewController:pickercustom animated:YES completion:nil]; } } 

Added "endGeneratingDeviceOrientationNotifications" to stop the rotation of the interface (which still worked fine).

I also tried adding these three methods after reading this: UIImagePickerController in iOS 6 does not work correctly

  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } - (BOOL)shouldAutorotate { return NO; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } 

... but this was perhaps the specific solution of iOS 6. In my case, it did not work.

Please let me know if you can find out the reason. It would be very helpful.

+4
source share
2 answers

You are close. If you want to maintain rotation, but want one viewController to not rotate, things get complicated:

The UIResponder chain REALLY wants the entire application to have the same rotation. Simply overriding rotation delegation methods in your separate class will not work. (FYI, in your case, you will need to subclass the UIImagePickerController to add these methods.) You will need to implement these delegate methods in the root navigation controller (you will need to have your own subclass again) and override them to request the topmost viewController for it rotation. Sort of:

 // Handles the should Auto Rotation for all view controllers - (BOOL)shouldAutorotate { if ([self.topViewController conformsToProtocol:@protocol(CustomRotation)]) { return [self.topViewController shouldAutorotate]; } // Auto rotate the screen by default. return YES; } // Handles the supported Interface Orientations for all View Controllers by seeing if // the top level viewController responds to Custom Rotation callbacks. - (NSUInteger)supportedInterfaceOrientations { if ([self.topViewController conformsToProtocol:@protocol(CustomRotation)]) { return [self.topViewController supportedInterfaceOrientations]; } // The default rotation for the application. return UIInterfaceOrientationMaskAll; } 

You cannot use respondsToSelector: instead of conformsToProtocol: because the select method will always return YES for any class that comes from the UIResponder (like everyone else), and you will have to override the rotation delegates on each UIViewController in your project to do this work. Instead, you can create an empty protocol ( CustomRotation ). Your custom rotation class requires this protocol and include overridden methods for delegating rotation, as you have above, with your desired limitations.

Finally, make sure that the supported interface orientations are set correctly in xcode and / or in your Application: didFinishLaunchingWithOptions .

+2
source

As for the R&D I made for the same theme, the Imagepicker camera in the iOS7 iPad has a default interface to change the orientation to the landscape. They designed the interface in this way. We cannot forcibly block its orientation.

If you still want to do this, you need to use a custom ImagePicker using AVCam https://developer.apple.com/library/ios/samplecode/avcam/Introduction/Intro.html

and custom image picker ... http://www.codza.com/custom-uiimagepickercontroller-camera-view

and force fix the orientation ...

+2
source

Source: https://habr.com/ru/post/1502372/


All Articles