【问题标题】:Disable rotation in UIImagePicker在 UIImagePicker 中禁用旋转
【发布时间】:2012-04-21 20:12:51
【问题描述】:

您好,我需要禁用 UIImagePicker 的旋转。
如果用户在 iPhone 处于“横向”模式时拍照,按钮将旋转,并且所拍摄的照片也会旋转。我想禁用该选项,因此加速度计将无法工作,并且它始终会像在纵向模式下一样拍照。
我该怎么做?
谢谢,马坦·拉多姆斯基。

【问题讨论】:

  • 您是要禁用整个应用的旋转还是只禁用这个视图?

标签: iphone objective-c ios5 uiimagepickercontroller


【解决方案1】:

将以下代码添加到您的视图中

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

【讨论】:

  • 它不起作用...基本上我想强制相机处于纵向模式
【解决方案2】:

早在 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]];

如果您只是想暂时抑制方向更改,则无法恢复。我们也无法判断窗口是否正在监视来自其他来源的任何重要通知。也是不完整的——翻身按钮,拍照按钮,自行移动和旋转。

你如何解决这个怪癖?您可以关闭 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];

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

【讨论】:

  • 如果这会导致我的应用程序被拒绝,那么我不能使用它。我需要将我的应用上传到应用商店。你知道如何做到这一点而不会让我的应用被拒绝吗?
  • 如果我有更好的解决方案,我会写 274 字关于这个吗?
  • 仔细看看您的问题 - 真正的问题是 UI 被旋转还是生成的图像?您是否只需要保证您保存的所有图片都是纵向的?这是可以实现的——如果宽度大于高度,只需将它们旋转 90 度即可。
  • 好吧,听着,我是改变我的脸的开发者,我已经在 iOS 的 flash air 中构建了它,然后我现在在 Xcode 中重新创建了它的问题,这就是它应该看起来的样子(我的鼠标躺在一边想象卷轴是一张脸。)i.imgur.com/mmyVh.png 看到了吗?它就像它站立时一样合适,但这就是保存图片以及用户想要保存或重新拍摄它时的样子i.imgur.com/1LmgD.png 看到了吗?它完全旋转了。你能帮帮我吗?
【解决方案3】:

这是解决方案:[UIDevice endGeneratingDeviceOrientationNotifications]

为什么这么难找?两个原因:

  1. UIDevice 记录打开或关闭方向通知的次数。如果开启次数多于关闭次数,仍会发出通知。
  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];

【讨论】:

  • 好吧,听着,我是改变我的脸的开发者,我已经在 iOS 的 flash air 中构建了它,然后我现在在 Xcode 中重新创建了它的问题,这就是它应该看起来的样子(我的鼠标躺在一边,想象卷轴是一张脸。)i.imgur.com/mmyVh.png 看到了吗?它就像站立时一样合适,但这就是保存图片以及用户想要保存或重新拍摄它时的样子i.imgur.com/1LmgD.png 看到了吗?它完全旋转了。你能帮帮我吗?
  • 我确实在这个答案的末尾提到了那个确切的问题。似乎没有办法阻止UIImagePickerController 在拍摄后旋转您的图像。但是,还有其他方法可以获取相机数据。涉及更多工作,但您可以更好地控制输出。见AVFoundation camera tutorial
  • 好吧....(发现 2011 年使用 iOS5 的新视频)在视频库中我不想创建自己的 GUI,但我必须:/ 非常感谢!跨度>
  • 你是我 1 小时搜索痛苦的完美解决方案!感谢分享!
  • 绝妙的解决方案!停止方向更改通知对我有用。 :)
【解决方案4】:

以上解决方案都不适合我。这是什么工作(对于我已经测试过的 iOS 7)-

  1. 子类UIImagePickerController

  2. 覆盖shouldAutorotate方法并返回NO

    -(BOOL)shouldAutorotate
    {
        return NO;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多