4

我想用一些相机功能编写应用程序,并使用覆盖视图来装饰图像。UIImagePickerController(使用相机作为源)在iPad2上自动旋转,我该如何阻止它?

我这是怎么实现的应用程序: 我使用的UIImagePickerController给谁什么相机拍摄的,然后将用户添加一个UIImageView到cameraOverlayView作为一个子视图,这样它的工作原理是这样的:
(图像在http://www.manna-soft.com/test/uploads/UIImagePickerView-portrait.jpg

这工作得很好,直到iPad2的进入的地方......它autorotates这样,破坏布局:
(在http://www.manna-soft.com/test/uploads/UIImagePickerView-landscape.jpg图像)

的永远的UIImagePickerController的iPhone,iPod touch或第一代iPad旋转,但它在我身上PAD2。 UIImagePickerContrller的类引用表示它“仅支持纵向模式”,但是会发生什么样的自动旋转......
有没有一种方法可以禁用自动旋转?
我试图在方法shouldAutorotateToInterfaceOrientation中返回NO:显示UIImagePickerController的视图控制器,但它仍然旋转。

在此先感谢。

+0

难道你碰巧发现这个 – Nash 2011-04-20 13:26:55

+0

我做了类似MarcVivet的一些建议的解决方案,使反转动画抗衡自转。我想我会在稍后尝试你的解决方案,因为看起来更加整洁这样做(我没有ipad2现在就尝试)。 – Eric 2011-04-27 11:28:47

回答

-3

您可以通过漫游UIImagePickerController的叠加视图来补偿您的ipad的旋转。最前一页,你必须捕捉使用的通知:

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

然后使用此代码:

- (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; 
     } 
    } 
} 
} 
+0

这就是我现在正在做的,尽管代码很长,但它为我完成了这项工作。我没有使用通知中心,而是在willRotateToInterfaceOrientation:duration:方法中实现了代码,以便我确切知道执行循环所需的时间。 – Eric 2011-04-27 11:23:08

+0

我试着用你的建议和Eric的这个,但都没有奏效。 在iPad 2上,我收到了很多UIDeviceOrientationDidChangeNotifications,每次旋转至少2次,旋转从未保持正常。 – 2011-06-02 23:10:47

+0

此代码仅适用于横向版权,如果您想使用纵向模式,您必须更改转换...¬_¬ – MarcVivet 2011-06-03 07:42:42

0

因为UIImagePickerController派生自UIViewController派生自UIViewController,您可以检查在“UIViewController文档”Handling View Rotations“,以查看该信息是否有帮助。

+0

感谢您的建议 – Eric 2011-04-18 09:45:15

+0

我已经尝试继承UIImagePickerController并覆盖方法shouldAutorotateToInterfaceOrientation:以便它只有在纵向模式下才会返回YES ...但它不起作用,该方法在调用控件时将其打印到控制台,我发现该方法从来没有被称为....任何想法为什么是这样的? – Eric 2011-04-18 10:58:09

5

叠加图可以添加到窗口,和THN的window.superview可以设置为cameraOverlayView。 关闭ModalController时,可以从窗口中删除叠加视图。

这个解决方案可能有点棘手,取决于您的应用程序的结构。

YourAppDelegate *appDelegate = (YourAppDelegate *) [[UIApplication sharedApplication] delegate]; 
[appDelegate.window addSubview:overlayView]; 
imagePickerController.cameraOverlayView = appDelegate.window.superview; 


//When dismissing the UIImagePicker 
[self dismissModalViewControllerAnimated:YES]; 
[OverlayView removeFromSuperview]; 
+0

我从未想过这个...如果我在窗口上添加子视图,它是否会永远不会旋转(因为窗口不旋转)? – Eric 2011-04-27 11:26:40

+0

这个解决方案适用于我,但上面的MarcVivet没有。谢谢! – 2011-06-02 23:09:25

0

你会发现,当你这样做:

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

结果将是YES。我认为默认情况下,它设置为YES。

你也可以继承的UIImagePickerController并添加此方法来覆盖方法:

- (BOOL)shouldAutorotate{ 
return NO; 
} 

然后,而不是使用的UIImagePickerController,用你创建的子类。

UIImagePickerSubclass *camera = [UIImagePickerSubclass new]; 

希望这有助于:)