UIImagePickerController (using the camera as a source) does autorotation on iPad2, how can I stop it?

I am trying to write an application with some camera function, and I use an overlay view to decorate it with an image.

Here's how I implement the application: I use the UIImagePickerController for the user who makes the camera, and adds the UIImageView to the OverlayView camera as a subquery so that it works as follows: (image http://www.manna-soft.com/test/ uploads / UIImagePickerView-portrait.jpg )

This works great until iPad2 falls into place ... it autorotates like this and destroys the layout:
(image http://www.manna-soft.com/test/uploads/UIImagePickerView-landscape.jpg )

UIImagePickerController never rotates on iphone, ipod touch or original iPad, but it works on iPad2. the reference to the UIImagePickerContrller class says that it "only supports portrait mode", but what happens, it autorotates like this ... Is there a way to disable autorotation? I tried to return NO in the shouldAutorotateToInterfaceOrientation: method from the view controller, which is represented by UIImagePickerController, but it still rotates.

Thanks in advance.

+4
source share
4 answers

You can compensate for the rotation of your ipad by doing a routine overlay view of your UIImagePickerController. To delete notifications, use:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationCallback:) name:nil object:nil]; 

Then use this code:

  - (void) notificationCallback:(NSNotification *) notification { if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { if ([[notification name] isEqualToString:@"UIDeviceOrientationDidChangeNotification"]) { UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; switch ( orientation ) { case UIInterfaceOrientationLandscapeRight: NSLog(@"LandcapeRight"); [UIView beginAnimations:@"LandscapeRight" context:UIGraphicsGetCurrentContext()]; [UIView setAnimationDuration:0.4]; m_uiCameraOverlayView.transform = CGAffineTransformIdentity; [UIView commitAnimations]; break; case UIInterfaceOrientationLandscapeLeft: NSLog(@"LandscapeLeft"); [UIView beginAnimations:@"LandcapeLeft" context:UIGraphicsGetCurrentContext()]; [UIView setAnimationDuration:0.4]; m_uiCameraOverlayView.transform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(M_PI), 0, 0); [UIView commitAnimations]; break; case UIInterfaceOrientationPortraitUpsideDown: NSLog(@"UpsideDown"); [UIView beginAnimations:@"UpsideDown" context:UIGraphicsGetCurrentContext()]; [UIView setAnimationDuration:0.4]; m_uiCameraOverlayView.transform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(-M_PI / 2), -128, -128); [UIView commitAnimations]; break; case UIInterfaceOrientationPortrait: NSLog(@"Portrait"); [UIView beginAnimations:@"Portrait" context:UIGraphicsGetCurrentContext()]; [UIView setAnimationDuration:0.4]; m_uiCameraOverlayView.transform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(M_PI / 2), 128, 128); [UIView commitAnimations]; break; default: NSLog(@"????"); break; } } } } 
-3
source

You can add an overlay view in the window, and window.superview can be set to cameraOverlayView. By rejecting the ModalController, the overlay view can be removed from the window.

This decision can be a little complicated depending on how your application is structured.

 YourAppDelegate *appDelegate = (YourAppDelegate *) [[UIApplication sharedApplication] delegate]; [appDelegate.window addSubview:overlayView]; imagePickerController.cameraOverlayView = appDelegate.window.superview; //When dismissing the UIImagePicker [self dismissModalViewControllerAnimated:YES]; [OverlayView removeFromSuperview]; 
+5
source

Since the UIImagePickerController comes from the UINavigationController, which comes from the UIViewController, you can check in the " Processing rotation views " in the UIViewController document to find out the information helps.

0
source

You will notice that when you do this:

 UIImagePickerController *camera = [UIImagePickerController new]; NSLog([self.camera shouldAutorotate] ? @"YES" : @"NO"); 

The result will be YES. I think it is set to YES by default.

You can subclass UIImagePickerController and add this method to override this method:

 - (BOOL)shouldAutorotate{ return NO; } 

Then, instead of using the UIImagePickerController, use the generated subclass.

 UIImagePickerSubclass *camera = [UIImagePickerSubclass new]; 

Hope this helps :)

0
source

All Articles