相机图像选择器控件 - iOS7中的自动旋转

时间:2013-09-16 09:36:49

标签: ios objective-c ipad ios7

我有一个照片应用程序覆盖相机图像选择器上的自定义按钮(用于拍照,打开/关闭闪光灯,其他常见的东西等)。

我希望控制界面仅支持纵向方向(我只讨论控制按钮/界面,而不是实际捕获的图像),这在iOS 6下工作正常。

然而,升级到xCode版本5.0并将我的iPad 3升级到iOS 7(GM Seed,用于iPad WiFi第3代),我发现当方向改变时,相机选择器界面会自动旋转。令人惊讶的是,我在iPhone 5(升级到iOS 7)上测试了相同的版本,但是自动旋转问题并没有表现出来。

[为了双重确定,我再次测试了iOS 6中的相同代码,并且在iPhone或iPad中都没有发生自动旋转]。

为了演示我如何处理我的图像选择器,这里有一些代码片段:

    UIImagePickerController *pickercustom = [[UIImagePickerController alloc] init];
    pickercustom.sourceType = UIImagePickerControllerSourceTypeCamera;
    pickercustom.showsCameraControls = NO;
    pickercustom.wantsFullScreenLayout = YES;
    pickercustom.navigationBarHidden=YES;
    pickercustom.view.userInteractionEnabled=YES;

    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {


    if (IPAD.userInterfaceIdiom == UIUserInterfaceIdiomPad)
    {
        pickercustom.delegate = self;
           UIDevice *currentDevice = [UIDevice currentDevice];
                    while ([currentDevice isGeneratingDeviceOrientationNotifications])
                        [currentDevice endGeneratingDeviceOrientationNotifications];


        [self presentViewController:pickercustom animated:YES completion:nil];

                    while ([currentDevice isGeneratingDeviceOrientationNotifications])
                        [currentDevice endGeneratingDeviceOrientationNotifications];

    }

    else
    {
        pickercustom.delegate = self;
        [self presentViewController:pickercustom animated:YES completion:nil];
    }
  }

添加了'endGeneratingDeviceOrientationNotifications'以阻止界面旋转(迄今为止工作正常)。

在阅读完这篇文章后,我也试过添加这三种方法:UIImagePickerController in iOS 6 doesn't work properly

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

  - (BOOL)shouldAutorotate
  {
    return NO;
    }

 - (NSUInteger)supportedInterfaceOrientations
  {
    return UIInterfaceOrientationMaskPortrait;
  }

...但这可能是iOS 6特定的解决方案。它在我的情况下不起作用。

如果你能找出根本原因,请告诉我。这将是很大的帮助。

2 个答案:

答案 0 :(得分:2)

你很亲密。当你想支持旋转但希望 一个 viewController不旋转时,事情变得棘手:

UIResponder链真的希望整个应用程序具有相同的轮换。只是简单地覆盖单个类中的旋转委托方法将不起作用。 (仅供参考,在您的情况下,您需要继承UIImagePickerController以添加这些方法。)您需要在根导航控制器中实现这些委托方法(您需要再次拥有自己的子类),以及覆盖它们以查询最顶层的viewController以获得所需的旋转。类似的东西:

// Handles the should Auto Rotation for all view controllers
- (BOOL)shouldAutorotate {

    if ([self.topViewController conformsToProtocol:@protocol(CustomRotation)]) {
        return [self.topViewController shouldAutorotate];
    }

    // Auto rotate the screen by default.
    return YES;
}

// Handles the supported Interface Orientations for all View Controllers by seeing if
// the top level viewController responds to Custom Rotation callbacks.
- (NSUInteger)supportedInterfaceOrientations {
    if ([self.topViewController conformsToProtocol:@protocol(CustomRotation)]) {
        return [self.topViewController supportedInterfaceOrientations];
    }

    // The default rotation for the application.
    return UIInterfaceOrientationMaskAll;
}

您无法使用respondsToSelector:代替conformsToProtocol:,因为对于从YES派生的任何类,选择器方法将始终返回UIResponder(所以,就像所有内容一样) ,并且您必须覆盖项目中每个UIViewController的旋转委托才能使其工作。相反,您可以创建一个空白协议(CustomRotation)。在您的自定义轮换类中,需要该协议并包含上面所覆盖的旋转委托方法,以及您所需的限制。

最后,确保在xcode和/或Application: didFinishLaunchingWithOptions方法中正确设置了支持的界面方向。

答案 1 :(得分:2)

就我在同一主题上所做的R& D而言,IOS7 iPad中的Imagepicker相机具有默认界面,可以改变横向方向。他们设计了这样的界面。我们无法强行锁定它的方向。

如果仍愿意,您必须使用自定义ImagePicker,请使用AVCam https://developer.apple.com/library/ios/samplecode/avcam/Introduction/Intro.html

和自定义图片选择器...... http://www.codza.com/custom-uiimagepickercontroller-camera-view

并强行锁定方向......

相关问题