加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – UIImagePickerController InterfaceOrientation

发布时间:2020-12-16 07:34:57 所属栏目:百科 来源:网络整理
导读:自从我的设备更新到6.1后,我在尝试显示UI ImagePickerController时遇到了崩溃.我只使用纵向方向. 碰撞: Reason: * Terminating app due to uncaught exception ‘UIApplicationInvalidInterfaceOrientation’,reason: ‘preferredInterfaceOrientationForPr
自从我的设备更新到6.1后,我在尝试显示UI ImagePickerController时遇到了崩溃.我只使用纵向方向.

碰撞:

Reason: * Terminating app due to uncaught exception ‘UIApplicationInvalidInterfaceOrientation’,reason: ‘preferredInterfaceOrientationForPresentation must return a supported interface orientation!’

这是我调用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 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

当然,您可以根据自己的需要修改它 – 使其自动旋转并返回多个支持的方向等.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读