1

我正在尝试UIImagePickerController使用以下代码在我的应用程序中打开一个对象:

   self.imgPicker = [[UIImagePickerController alloc] init];
   self.imgPicker.delegate = self;
   self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
   self.imgPicker.modalPresentationStyle = UIModalPresentationFullScreen;
   [self presentViewController:self.imgPicker animated:YES completion:nil];

但随后应用程序崩溃并显示以下错误:

Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 
'preferredInterfaceOrientationForPresentation must return a supported interface orientation!'

我还在ViewController课堂上添加了以下方法

- (BOOL) shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:    (UIInterfaceOrientation)toInterfaceOrientation {

    return UIInterfaceOrientationMaskPortrait;
}

那么,我还应该做些什么来呈现这个UIImagePickerController呢?

4

4 回答 4

1

这似乎是因为选择照片 VC 没有实现新的定位方法。要解决这个问题,请确保视图控制器确实实现了它们,即使顶级 VC 没有。

在代码中:

@implementation UINavigationController (Rotation_IOS6)
-(BOOL)shouldAutorotate {
    return [self.topViewController shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations {
    UIInterfaceOrientationMask topControllerOrientationMask = [self.topViewController supportedInterfaceOrientations];
    return topControllerOrientationMask ? topControllerOrientationMask : UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    UIInterfaceOrientation topControllerOrientation = [self.topViewController preferredInterfaceOrientationForPresentation];
    return topControllerOrientation ? topControllerOrientation : UIInterfaceOrientationPortrait;
}
@end
于 2013-08-12T10:19:14.673 回答
0

关于崩溃问题,您可以阅读这里

shouldAutorotateToInterfaceOrientation必须返回不是UIInterfaceOrientationMaskPortrait,但是UIInterfaceOrientationPortrait,关于你可以阅读这里的差异

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return UIInterfaceOrientationPortrait;
}
于 2013-08-12T07:30:25.243 回答
0

我认为您尝试在 iPad 上启动您的应用程序的图像选择器。如果是这样,您将无法在 iPad 上启动这种类型的选择器,它仅适用于 iPhone。要在 iPad 上使用 ImagePicker,请使用 UIPopOver 启动 imagePicker。

于 2013-08-12T07:37:36.487 回答
0

几天前我有同样的问题。我发现了以下技巧。

将此代码放入您的

AppDelegate.m

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return UIInterfaceOrientationMaskAll;
}

并将此代码放入您的UIImagePickerViewController课程中。

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}

因为在iOS-6 UIImagePickerController必须遵循纵向。

于 2013-08-12T08:47:55.033 回答