在iPad中使用UIImagePicker,仅在iOS 6上使用横向

在iPad中使用UIImagePicker,仅在iOS 6上使用横向,ipad,ios6,orientation,uiimagepickercontroller,landscape,Ipad,Ios6,Orientation,Uiimagepickercontroller,Landscape,我正在制作一个只支持横向的iPad应用程序,在该应用程序中,我使用以下代码调用照片库 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker.mediaTypes = [N

我正在制作一个只支持横向的iPad应用程序,在该应用程序中,我使用以下代码调用照片库

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObjects: (NSString *) kUTTypeImage, nil];

self.iPadPopoverController = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[self.iPadPopoverController setDelegate:self];
[self.iPadPopoverController presentPopoverFromRect:CGRectMake(490, 638, 44, 44) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
但是,这会导致崩溃,但以下情况除外:

Uncaught exception: Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES
坠机原因:

经过研究,我发现崩溃是由于UIImagePickerController始终以纵向模式显示,即使界面方向为横向

我是如何试图解决这个问题的:

看完报纸后 以及之前发布的几个问题;我发现引发崩溃的方法是应用程序委托的

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
显而易见的解决方案是让它返回UIInterfaceOrientationMaskLandscape | UIInterfaceOrientationMaskPortrait

但是,我希望我的应用程序只支持风景!因此,我的第一个解决方案是向应用程序委托添加一个布尔标志,并在调用UIImagePickerController之前将其设置为“是”,然后在关闭它之后将其设置为“否”

这很有效,但我对它不满意,觉得这是一个混乱的解决方案。因此,我让该方法检查其调用者,如果它是初始化UIImagePickerController的函数,则该方法将返回UIInterfaceOrientationMaskPortrait

该解决方案在模拟器和设备上都有效

这是奇怪的部分

在将我的应用程序提交到商店后,我的所有用户都报告说,每当他们访问照片库时,应用程序都会崩溃。我查看了坠机报告,发现坠机是由于上述异常情况造成的。我不明白为什么会这样

所以我就这个问题向苹果提交了一份技术支持事件。他们回答说,应用程序存档后,堆栈符号不是人类可读的,这就是为什么我的检查总是失败的原因

他们还建议在设备上进行测试,首先将项目归档到.ipa文件中,然后通过iTunes安装,以便在将来检测类似问题

那很好,但我的问题仍然存在

有人知道如何解决这个问题吗

在info.plist中,声明支持的所有方向

您不再需要application:SupportedInterfaceDirectionsforWindow:方法,因此可以将其删除

子类根视图控制器

添加以下方法:

- (BOOL)shouldAutorotate {
    return YES;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
        return self.interfaceOrientation;
    } else {
        return UIInterfaceOrientationLandscapeRight;
    }
}
如果您提供了模态视图控制器,并且不希望它们被旋转,那么您也需要将此代码添加到它们的根控制器中

- (BOOL)shouldAutorotate {
    return YES;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
        return self.interfaceOrientation;
    } else {
        return UIInterfaceOrientationLandscapeRight;
    }
}