在横向使用UIImagePickerController

时间:2013-10-15 06:01:01

标签: ios objective-c uiimagepickercontroller landscape uiimageorientation

我正在创建一个处于横向模式的应用,我正在使用UIImagePickerController使用iPhone相机拍摄照片,我也希望以横向模式创建照片。

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

8 个答案:

答案 0 :(得分:47)

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

- (BOOL)shouldAutorotate
{
    return NO;
}

使用:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}

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

enter image description here

但请务必检查部署信息中的肖像:

enter image description here

答案 1 :(得分:27)

  

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

一行代码可以带来很大的不同!在您的IBAction登陆的方法或功能中:

在斯威夫特,

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

目标-C,

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

注意:这将允许imagePickerController正确显示它的视图,但 可能无法在呈现时解决旋转问题。

答案 2 :(得分:14)

试试这种方式......

  

根据Apple Document,ImagePicker Controller从不在横向模式下旋转。您只能在纵向模式下使用。

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

在ViewController.m中:

制作Image Picker Controller的SubClass(NonRotatingUIImagePickerController)

@interface NonRotatingUIImagePickerController : UIImagePickerController

@end

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

使用方法如下

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

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

答案 3 :(得分:7)

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

import UIKit

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

只需将扩展名放在项目的某个位置,无需为了它的工作而继承任何子类。

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

import UIKit

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

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

答案 4 :(得分:6)

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

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

  override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .all
  }
}

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

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

答案 5 :(得分:5)

在横向模式下使用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];
}

答案 6 :(得分:3)

修改上述代码方法

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

答案 7 :(得分:2)

接受的答案对我不起作用。我还要将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];

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

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

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