UIImagePickerController (using camera as source) does autorotate on iPad2, how do i stop it?

筅森魡賤 提交于 2019-12-04 11:46:19

You can compensate the rotation of your ipad by roatting the overlay view of your UIImagePickerController. Fisrt you have to capture the notifications using:

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

The overlay view can be added to the window, and thn the window.superview can be set as cameraOverlayView. While dismissing the ModalController the overlay view can be removed from the window.

This solution can be a little tricky to apply depending on how your app 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]; 

Because UIImagePickerController derives from UINavigationController which derives from UIViewController, you can check out at "Handling View Rotations" in the UIViewController doc to see if that info helps.

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 by default, it is set to YES.

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

- (BOOL)shouldAutorotate{
return NO;
}

Then instead of using UIImagePickerController, use your created subclass.

UIImagePickerSubclass *camera = [UIImagePickerSubclass new];

Hope this helps :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!