2012-04-09 51 views
13

你好,我需要禁用UIImagePicker的旋转。
如果用户在iPhone处于“横向”状态时拍摄照片,则按钮将旋转,拍摄的照片也将旋转。我想禁用该选项,以使加速度计不起作用,并且它总是会以纵向模式拍照。
我该怎么做?
谢谢,马坦拉多姆斯基。在UIImagePicker中禁用旋转

+0

你想禁用旋转整个应用程序或只是对此有何看法? – Will 2012-04-09 12:59:06

回答

27

以下是解决方案:[UIDevice endGeneratingDeviceOrientationNotifications]

为什么很难找到?原因有两个:

  1. 记录定向通知打开或关闭的次数。如果已关闭的次数超过关闭次数,通知仍会发出。
  2. UIImagePickerController将这些通知转换为何时显示。

因此,调用此方法曾对图像选择器没有任何作用。为了确保方向通知关闭,并且保持关闭,您需要在提供选取器之后在之间关闭它们。

这不会影响iOS或其他应用程序。它甚至不会完全影响您自己的应用程序:与我建议的其他方法一样,相机按钮会继续响应方向更改,并且拍摄的照片也可以识别方向。这很奇怪,因为如果不需要设备定位硬件,它应该被关闭。

@try 
{ 
    // Create a UIImagePicker in camera mode. 
    UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease]; 
    picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
    picker.delegate = self; 

    // Prevent the image picker learning about orientation changes by preventing the device from reporting them. 
    UIDevice *currentDevice = [UIDevice currentDevice]; 

    // The device keeps count of orientation requests. If the count is more than one, it continues detecting them and sending notifications. So, switch them off repeatedly until the count reaches zero and they are genuinely off. 
    // If orientation notifications are on when the view is presented, it may slide on in landscape mode even if the app is entirely portrait. 
    // If other parts of the app require orientation notifications, the number "end" messages sent should be counted. An equal number of "begin" messages should be sent after the image picker ends. 
    while ([currentDevice isGeneratingDeviceOrientationNotifications]) 
     [currentDevice endGeneratingDeviceOrientationNotifications]; 

    // Display the camera. 
    [self presentModalViewController:picker animated:YES]; 

    // The UIImagePickerController switches on notifications AGAIN when it is presented, so switch them off again. 
    while ([currentDevice isGeneratingDeviceOrientationNotifications]) 
     [currentDevice endGeneratingDeviceOrientationNotifications]; 
} 
@catch (NSException *exception) 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Camera" message:@"Camera is not available" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
} 

如上所述,拍摄的照片可能仍然存在错误的方向。如果您希望它们的定向一致,请检查它们的纵横比并相应旋转。我推荐this answer to How to Rotate a UIImage 90 degrees?

//assume that the image is loaded in landscape mode from disk 
UIImage * landscapeImage = [UIImage imageNamed: imgname]; 
UIImage * portraitImage = [[UIImage alloc] initWithCGImage: landscapeImage.CGImage scale:1.0 orientation: UIImageOrientationLeft] autorelease]; 
+0

好吧,听我是改变我的脸的开发人员我已经在iOS的Flash空气中构建它,并且现在我在Xcode中重新创建了它,因为这个问题看起来应该是这样的(我的鼠标躺在一边想象该滚动是一张脸。)http://i.imgur.com/mmyVh.png见?它恰如其分地站立着,但是这是当图片被保存并且用户想要保存或重新获得时的样子http://i.imgur.com/1LmgD.png看到了吗?它完全旋转。你能帮我吗? – user1321887 2012-04-17 14:32:30

+0

我在这个答案的最后提到了确切的问题。在拍摄后,似乎没有办法阻止'UIImagePickerController'旋转图像。但是,还有其他方法可以获取相机数据。涉及更多的工作,但是你对输出有更多的控制。请参阅[AVFoundation相机教程](http://stackoverflow.com/q/3643445/1318452)。 – Dondragmer 2012-04-17 22:22:09

+0

好吧....(在iOS5中发现2011的新视频)在视频库中我不想创建自己的GUI,但我必须:/非常感谢! – user1321887 2012-04-18 17:51:06

-3

添加以下代码到您的视图

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return NO; 
} 
+0

它dosent工作...基本上我想强制相机处于肖像模式 – user1321887 2012-04-10 17:45:39

+0

根本不工作 - 没有任何影响的影像挑战 – 2012-08-13 13:38:10

+0

不起作用,它只停止视图控制器旋转 – Agata 2012-12-24 11:40:00

3

早在2010年,How to prevent a modal UIImagePickerController from rotating?没有发现幸福这个问题的答案。

我找到了一个替代方案,但它也想让你从App Store中被拒绝:从窗口中删除所有观察者。

如果一个的viewController外应用的某些部分需要方向变化的消息,它可以为他们登记:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]]; 

的窗口,这并不奇怪,注册了这样的通知,然后旋转视图控制器,如果他们接受新的方向。这是不方便观看UIDeviceOrientationDidChangeNotification,所以必须有一个私人命名的等值。所有你能做的就是从窗口中删除通知:

[[NSNotificationCenter defaultCenter] removeObserver:[[self view] window]]; 

没有恢复这一点,如果你只是想暂时抑制取向的变化。我们也不能分辨窗口是否正在监视其他来源的任何重要通知。它也不完整 - 用于翻转的按钮以及用于拍摄照片的按钮,可以自行移动和旋转。

Screenshot of iPad in landscape mode with camera UI still in portrait

你怎么能解决这个怪癖?您可以在UIImagePicker上关闭showsCameraControls,并为其自己的cameraOverlayView提供相机按钮。或继续通过遍历从一切删除通知的整个视图层次结构(也排斥材料)随机移除通知:

- (void) removeAllNotificationsFromView:(UIView*) view; 
{ 
    // This recurses through the view hierarchy removing all notifications for everything. 
    // This is potentially destructive, and may result in rejection from the App Store. 
    [[NSNotificationCenter defaultCenter] removeObserver:view]; 
    for (UIView *subview in [view subviews]) 
     [self removeAllNotificationsFromView:subview]; 
} 

[self removeAllNotificationsFromView:imagePicker.view]; 

我不建议用这个,但它提供了一些有趣的见解方向如何变化检测和传播。

+0

如果这会导致我的应用程序被拒绝比我不能使用这个。我需要将我的应用上传到应用商店。你知道一种方法来做到这一点,而不会让我的应用程序被拒绝? – user1321887 2012-04-14 14:41:05

+1

如果我有更好的解决方案,我会写274字关于这个吗? – Dondragmer 2012-04-14 15:11:35

+0

仔细看看你的问题 - 用户界面正在旋转的真正问题,还是由此产生的图像?你只需要保证你保存的所有图片都是纵向的?这是可以实现的 - 只要将它们旋转90度,如果它们的宽度大于它们的高度。 – Dondragmer 2012-04-14 15:12:42

1

上述解决方案都不适用于我。什么工作是这样的(适用于iOS 7,我已经测试了) -

  1. 子类UIImagePickerController

  2. 覆盖shouldAutorotate方法并返回NO

-(BOOL)shouldAutorotate 
    { 
     return NO; 
    } 
+1

未适用于iOS 8 – jonypz 2014-11-20 19:41:04

+0

适用于我的iOS8 – LetynSOFT 2015-03-16 08:50:55

+1

不适用于我iOS 9,10,11 – iMHitesh 2017-09-13 11:00:42