【问题标题】:UIImagePickerController InterfaceOrientation CrashUIImagePickerController 接口方向崩溃
【发布时间】:2013-03-20 21:25:03
【问题描述】:

自从我的设备更新到 6.1 后,我在尝试显示 UIImagePickerController 时遇到了崩溃。我只使用纵向。

崩溃:

原因:* 由于未捕获的异常“UIApplicationInvalidInterfaceOrientation”而终止应用程序,原因:“preferredInterfaceOrientationForPresentation 必须返回受支持的界面方向!”

这里是我调用 UIImagePickerController 的地方:

if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
    //The device cannot make pictures
    [PMAlertDialog showWithTitle:NSLocalizedString(@"incompatibleDeviceDialogTitle", nil) message:NSLocalizedString(@"incompatibleDeviceDialogMessage", nil) andButtonTitle:NSLocalizedString(@"okButtonTitle", nil)];
    return;
}

if (_imagePicker == nil)
{
    _imagePicker = [[UIImagePickerController alloc] init];
    _imagePicker.delegate = self;
}

_imagePicker.allowsEditing = NO;
_imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
_imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];

[self presentModalViewController:_imagePicker animated:YES];

我已将这些方法添加到添加 UIImagePickerController 的视图控制器中:

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

【问题讨论】:

    标签: objective-c uiimagepickercontroller uiinterfaceorientation


    【解决方案1】:

    为了解决这个问题,我做了如下分类:

    我创建了一个新的objective-c类,“UIImagePickerController+NonRotating”

    在头文件(UIImagePickerController+NonRotating.h)中:

    #import <Foundation/Foundation.h>
    
    @interface UIImagePickerController (NonRotating)
    
    - (BOOL)shouldAutorotate;
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
    
    @end
    

    在实现文件(UIImagePickerController+NonRotating.m)中:

    #import "UIImagePickerController+NonRotating.h"
    
    @implementation UIImagePickerController (NonRotating)
    
    - (BOOL)shouldAutorotate {
        return NO;
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
        return UIInterfaceOrientationPortrait;
    }
    
    @end
    

    您当然可以根据自己的喜好对其进行修改 - 使其自动旋转并返回多个支持的方向等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-13
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多