Iphone 禁用UIImagePicker中的旋转

Iphone 禁用UIImagePicker中的旋转,iphone,objective-c,ios5,uiimagepickercontroller,Iphone,Objective C,Ios5,Uiimagepickercontroller,您好,我需要禁用UIImagePicker的旋转。 如果用户在iPhone处于“横向”状态时拍照,按钮将旋转,拍摄的照片也将旋转。我想禁用该选项,这样加速计将无法工作,并且它将始终像在纵向模式下一样拍照。 我该怎么做? 谢谢,Matan Radomski。将以下代码添加到您的视图中 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return N

您好,我需要禁用UIImagePicker的旋转。
如果用户在iPhone处于“横向”状态时拍照,按钮将旋转,拍摄的照片也将旋转。我想禁用该选项,这样加速计将无法工作,并且它将始终像在纵向模式下一样拍照。
我该怎么做?

谢谢,Matan Radomski。

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
     return NO;
}
回到2010年,这个问题没有找到令人满意的答案

我找到了一个替代方案,但它也像是让你被应用商店拒绝:从窗口中删除所有观察者

如果viewController之外的应用程序的某些部分需要方向更改消息,它可以注册这些消息:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];
毫不奇怪,窗口会注册此类通知,然后在接受新方向时旋转视图控制器。它无法方便地监视
UIDeviceOrientationIDChangeNotification
,因此必须有一个私人命名的等价物。您所能做的就是从窗口中删除每个通知:

[[NSNotificationCenter defaultCenter] removeObserver:[[self view] window]];
如果您只是想暂时抑制方向更改,则无法恢复此操作。我们也无法判断窗口是否正在监视来自其他来源的任何重要通知。它也是不完整的——翻身的按钮和拍照的按钮,可以自己移动和旋转

你怎么能解决这个怪癖?您可以关闭UIImagePicker上的
ShowScameraControl
,并使用其上的摄像头按钮提供自己的
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];
我不建议使用它,但它提供了一些关于如何检测和传播方向变化的有趣见解。

以下是解决方案:

为什么这么难找到?原因有二:

  • ui设备
    记录打开或关闭方向通知的次数。如果打开次数超过关闭次数,仍将发出通知
  • 当显示这些通知时,
    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];
    }
    
    如上所述,拍摄的照片可能仍然处于错误的方向。如果希望它们的方向一致,请检查它们的纵横比并相应地旋转。我推荐


    上述解决方案对我都不起作用。这是什么工作(对于我已经测试过的iOS 7)——

  • 子类
    UIImagePickerController

  • 重写
    shouldAutorotate
    方法并返回
    NO


  • 你想禁用整个应用程序的旋转还是仅此视图?它不起作用。。。基本上,我想强制相机处于纵向模式,如果这会导致我的应用程序被拒绝,那么我不能使用它。我需要将我的应用程序上载到应用程序商店。你知道一种不让我的应用被拒绝的方法吗?如果我有更好的解决方案,我会为此写274个字吗?仔细看看你的问题——真正的问题是旋转的UI,还是由此产生的图像?您是否只需要保证您保存的所有图片都是纵向的?这是可以实现的-只要将它们旋转90度,如果它们的宽度大于高度。好的,听着,我是“改变我的脸”的开发者,我已经在flash air for iOS中创建了它,然后我现在在Xcode中重新创建了它。问题是,这是它应该是什么样子的(我的鼠标放在一边,想象滚动条是一张脸。)看到了吗?它就像是站着的一样合适,但这就是图片保存时的样子,以及用户希望保存或重新拍摄时的样子。看到了吗?它是完全旋转的。你能帮我吗?好的,听着,我是“改变我的脸”的开发者,我在flash air for iOS中创建了它,然后我现在在Xcode中重新创建了它,因为问题是它应该是什么样子(我的鼠标放在一边,想象卷轴是一张脸。)看到了吗?它就像是站着的一样合适,但这就是图片保存时的样子,以及用户希望保存或重新拍摄时的样子。看到了吗?它是完全旋转的。你能帮帮我吗?我确实在回答的最后提到了这个问题。似乎无法阻止
    UIImagePickerController
    在拍摄图像后旋转图像。但是,还有其他获取相机数据的方法。需要做更多的工作,但您对输出有更多的控制权。看,好吧。。。。(通过iOS5发现了2011年的新视频)在视频库中,我不想创建自己的GUI,但我必须:/嗯,非常感谢!你是我1小时搜索痛苦的完美解决方案!谢谢分享!很好的解决方案!停止方向更改通知对我有效。:)
    //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];
    
       -(BOOL)shouldAutorotate
        {
            return NO;
        }