ios 横向使用 UIImagePickerController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19374237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:20:13  来源:igfitidea点击:

Using UIImagePickerController in landscape orientation

iosobjective-cuiimagepickercontrollerlandscapeuiimageorientation

提问by Paras Gorasiya

I am creating an app which is in landscape mode and I am using UIImagePickerControllerto take photos using iPhone camera in it and I want to create it in landscape mode too.

我正在创建一个横向模式的应用程序,我正在UIImagePickerController使用其中的 iPhone 相机拍照,我也想以横向模式创建它。

But as the Apple documention suggests UIImagePickerControllerdoes not support landscape orientation, so what should I do to get desired functionality?

但是正如 Apple 文档所暗示的UIImagePickerController那样不支持横向,那么我应该怎么做才能获得所需的功能?

回答by Mc.Lover

If you'd like to use UIImagePickerController in landscape mode, use user1673099's answer, but instead of:

如果您想在横向模式下使用 UIImagePickerController,请使用user1673099's answer,而不是:

- (BOOL)shouldAutorotate
{
    return NO;
}

use:

用:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}

and then the picker would open in landscape mode:

然后选择器将以横向模式打开:

enter image description here

在此处输入图片说明

But make sure you check Portrait in deployment info:

但请确保在部署信息中检查 Portrait :

enter image description here

在此处输入图片说明

回答by David

... and I want to create it in landscape mode too.

...我也想以横向模式创建它。

One line of code can make a big difference! In the method or function where your IBAction lands:

一行代码可以产生很大的不同!在您的 IBAction 登陆的方法或函数中:

In Swift,

在斯威夫特,

let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
// .overCurrentContext allows for landscape and portrait mode
imagePickerController.modalPresentationStyle = .overCurrentContext

Objective-C,

目标-C,

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
[imagePickerController setDelegate:self];
[imagePickerController setModalPresentationStyle: UIModalPresentationOverCurrentContext];

Note: This will allow imagePickerController to present it's view correctly, but willmaynot fix the issue of rotation while it is presented.

注意:这将允许imagePickerController正确地呈现它的观点,但是可以同时呈现不解决旋转的问题。

回答by user1673099

Try this way....

试试这个方法....

As per Apple Document, ImagePicker Controller never Rotate in Landscape mode. You have to use in Portrait Mode only.

根据 Apple 文档,ImagePicker 控制器永远不会在横向模式下旋转。您必须仅在纵向模式下使用。

For disable Landscape mode only for ImagePicker Controller follow below code:

仅对 ImagePicker 控制器禁用横向模式,请遵循以下代码:

In your ViewController.m:

在您的 ViewController.m 中:

Make the SubClass(NonRotatingUIImagePickerController) of Image Picker Controller

制作 Image Picker Controller 的 SubClass(NonRotatingUIImagePickerController)

@interface NonRotatingUIImagePickerController : UIImagePickerController

@end

@implementation NonRotatingUIImagePickerController
// Disable Landscape mode.
- (BOOL)shouldAutorotate
{
    return NO;
}
@end

Use as follow

使用如下

UIImagePickerController* picker = [[NonRotatingUIImagePickerController alloc] init];
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.delegate = self; 
  etc.... Just as Default ImagePicker Controller

This is working for me & Let me know if you have any Problem.

这对我有用,如果您有任何问题,请告诉我。

回答by CodeBender

This works great with Swift 4.0in iOS 10/11.

这适用于 iOS 10/11 中的Swift 4.0

import UIKit

extension UIImagePickerController {
    override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .all
    }
}

Just drop the extension somewhere in your project, no need to subclass anything for it to work.

只需将扩展放在项目中的某个位置,无需子类化任何内容即可工作。

If you do need to specify device types, you can add a check like this:

如果确实需要指定设备类型,可以添加如下检查:

import UIKit

extension UIImagePickerController {
    override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return UIDevice.current.userInterfaceIdiom == .phone ? .portrait : .all
    }
}

This will allow an iPad to freely rotate, but enforces portrait mode on a phone. Just make sure that your app is configured to support these in its info.plist, otherwise you may encounter crashes upon launching the picker.

这将允许 iPad 自由旋转,但在手机上强制使用纵向模式。只需确保您的应用程序配置为在其 info.plist 中支持这些,否则您可能会在启动选择器时遇到崩溃。

回答by Senseful

Here's a version that supports rotation in all interface orientations:

这是一个支持所有界面方向旋转的版本:

/// Not fully supported by Apple, but works as of iOS 11.
class RotatableUIImagePickerController: UIImagePickerController {

  override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .all
  }
}

This way if the user rotates her device, it'll update the picker controller to support the current orientation. Just instantiate as you normally would a UIImagePickerController.

这样,如果用户旋转她的设备,它将更新选择器控制器以支持当前方向。只需像往常一样实例化 UIImagePickerController。

If you only want to support a subset of orientations, you can return a different value.

如果您只想支持方向的子集,您可以返回不同的值。

回答by iain

The correct way to use UIImagePickerControllerin landscape mode without any hacks is to put it into a UIPopoverController

UIImagePickerController在没有任何技巧的情况下在横向模式下使用的正确方法是将其放入UIPopoverController

- (void)showPicker:(id)sender
{
    UIButton *button = (UIButton *)sender;
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    _popover = [[UIPopoverController alloc] initWithContentViewController:picker];
    [_popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

回答by Abhishek Thapliyal

Modify above code method

修改上面的代码方法

- (NSUInteger)supportedInterfaceOrientations
 {
     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
     if(orientation == UIDeviceOrientationLandscapeRight || orientation == UIDeviceOrientationLandscapeLeft)
         return UIInterfaceOrientationMaskLandscape;
     else
         return UIInterfaceOrientationMaskPortrait;
 }

回答by Michal Cichon

Accepted answer doesn't work for me. I had also to add modalPresentationStyle to UIImagePickerController to make it working.

接受的答案对我不起作用。我还必须将 modalPresentationStyle 添加到 UIImagePickerController 以使其工作。

UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
pickerController.modalPresentationStyle = UIModalPresentationCurrentContext; //this will allow the picker to be presented in landscape
pickerController.delegate = self;
pickerController.allowsEditing = YES;
pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:pickerController animated:YES completion:nil];

And of course remember to put this in a controller that presents the picker:

当然记得把它放在一个显示选择器的控制器中:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape; //this will force landscape
}

But according to Apple's documentation this is not supported to present this picker in the landscape mode so be careful about it.

但是根据 Apple 的文档,不支持在横向模式下显示此选择器,因此要小心。