【问题标题】:UIImagePickerController in Landscape横向 UIImagePickerController
【发布时间】:2011-01-06 05:18:55
【问题描述】:

我一直在寻找这个问题的答案,但什么都想不出来。显然,iPhone SDK 3.0 使得 UIImagePickerController 可以在横向模式下显示 - 但我没有找到任何允许这样做的方法。我认为如果应用程序默认处于横向状态,它会自动调整图像控制器,但这对我不起作用。

感谢您的帮助!

【问题讨论】:

  • 鉴于内置照片应用程序的图像选择器不支持横向,您拥有横向图像选择器的机会很小,我想不出一种 SDK 安全的方式来获取内容相册的…
  • 正如 KennyTM 所提到的,内置的照片应用程序在横向模式下不起作用。是什么让您认为 SDK 3.0 可以在横向模式下使用选取器?也许如果你与我们分享它会给我们一个关于如何去做的提示。

标签: iphone cocoa-touch image uiimagepickercontroller landscape


【解决方案1】:

不需要子类化;只需覆盖 modalPresentationStyle 属性即可。

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.modalPresentationStyle = UIModalPresentationFormSheet;
    [viewController presentViewController:picker animated:YES completion:NULL];

【讨论】:

  • 我会将此作为已接受的答案。使用picker.modalPresentationStyle = UIModalPresentationCurrentContext; 以获得更好的结果。
  • @LeXeR 效果很好!此外,UIModalPresentationCustom 将修复 iPad 上的横向显示。
  • swift:picker.modalPresentationStyle = .currentContext
【解决方案2】:

我没有检查这是否非法,但它对我有用。 如果您希望 UIImagePickerController 在横向代码中启动(并保持):

//Initialize picker

UIImagePickerController * picker = [[UIImagePickerController alloc] init];
   picker.delegate = self;


//set Device to Landscape. This will give you a warning. I ignored it.
//warning: 'UIDevice' may not respond to '-setOrientation:'


[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

//Set Notifications so that when user rotates phone, the orientation is reset to landscape.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

//Refer to the method didRotate:   
[[NSNotificationCenter defaultCenter] addObserver:self
              selector:@selector(didRotate:)
               name:@"UIDeviceOrientationDidChangeNotification" object:nil];

//Set the picker source as the camera   
picker.sourceType = UIImagePickerControllerSourceTypeCamera;

//Bring in the picker view   
[self presentModalViewController:picker animated:YES];

方法didRotate:

- (void) didRotate:(NSNotification *)notification

{
      //Maintain the camera in Landscape orientation
 [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

}

【讨论】:

  • 您好erastusnjuki:感谢您的回复!这可以在 UIViewController 子类中实现,还是需要其他东西?
  • 它应该虽然我没有测试过。我在一个采用 的 UITableViewController 中实现了我的。
  • 在 iPhone 4 启动期间获取数据格式化程序暂时不可用错误。
  • "这会给你一个警告。我忽略了它。"不过,Apple 可能对您使用未记录的 API 并不满意
  • 我设法让选择器以横向显示,但是相机(实时)图像旋转了 90°,所以你不能用它来拍摄任何照片,就像你移动 iPhone 一样水平您将在相机屏幕/图像中看到垂直移动,反之亦然。其他人是否面临这个问题,甚至有解决这个问题的方法?提前感谢您的帮助! :)
【解决方案3】:

如果您只需要摆脱警告尝试

@interface UIDevice ()
    -(void)setOrientation:(UIDeviceOrientation)orientation;
@end

【讨论】:

  • @DmitryKhryukin,这是一个私有 API,几乎肯定会在 App Store 应用程序中被拒绝(除非您想尝试混淆 setOrientation: 选择器,然后将其潜入。)跨度>
  • @Nate 是的,我明白这一点。这就是我问这些问题的原因。
  • @DmitryKhryukin,这就是我回答它的原因。 Apple 不会接受这一点,除非您可以隐藏自己的使用方式。
  • 收到警告 - 从枚举类型“枚举 UIInterfaceOrientation”隐式转换为不同枚举类型“UIDeviceOrientation”(又名“枚举 UIDeviceOrientation”)
【解决方案4】:

我在横向模式下开发了一个 UIImagePicker 类。非常适合我开发的应用程序:希望它也适合你:

GitHub:https://github.com/imaginaryunit/iOSLandscapeThreadedImagePicker.git

【讨论】:

    【解决方案5】:

    我也有一个使用 UIImagePickerController 的全横向应用程序。请注意,如果您在横向模式下调用 UIImagePickerController,您的应用可能会被 Apple Review Team 拒绝。

    我针对这个问题设计了一个简单的工作,它使用了 shouldAutoRotate 委托。 Apple 批准将此方法用于全横向应用程序。

    查看here了解详细信息和可下载的完整项目源代码。

    【讨论】:

    • 补充一下,iOS4以后这个问题已经不存在了。 ImagePickerController 会自动以纵向格式显示自己,即使您处于横向状态。
    【解决方案6】:

    制作UINavigationController的类别并添加此方法

    - (BOOL)shouldAutorotate
    {
        return NO;
    }
    

    【讨论】:

      【解决方案7】:

      子类UIImagePickerController 并覆盖modalPresentationStyle,如下所示:

      - (UIModalPresentationStyle)modalPresentationStyle
      {
          if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
          {
              return UIModalPresentationFormSheet;
          }
      
          return [super modalPresentationStyle];
      }
      

      图像选择器现在是表单,不再处于全屏模式,但在横向模式下看起来不错。 这应该是完全应用商店安全的。

      这适用于画廊,不适用于拍照。

      【讨论】:

        【解决方案8】:

        我按如下方式解决了这个问题:每次改变方向后,我都会简单地重新创建选择器。尝试这个。不同的是太歪了……

        【讨论】:

        • 您好 Alexandr:即使我在应用程序设置为仅在横向模式下运行时创建 UIImagePickerController,它仍然显示为垂直模式。
        • iOS 7 是主要问题!在 iOS 6 中一切正常
        【解决方案9】:

        我正在开发一个尽可能在横向模式下工作的类。 在 GitHub 上查看:RACameraController

        【讨论】:

        • 我知道 3 年后,但您的项目显示黑屏。只是让你知道。
        • 谢谢,我会把它添加到我的 ToDo 列表中:)
        猜你喜欢
        • 1970-01-01
        • 2013-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-12
        • 1970-01-01
        相关资源
        最近更新 更多