在iPhone中使用iOS 7打开相机时,横向模式中的方向问题

我有一个只在横向模式的应用程序。 在我的应用程序,我从我的意见之一打开相机。 它适用于我的iPad,但在iPhone上它崩溃。 它在iOS 6中运行良好,但该应用程序崩溃的iOS 7和只为iPhone。

以下是我的代码。

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { ipc=[[UIImagePickerController alloc] init ]; ipc.delegate=self; ipc.sourceType=UIImagePickerControllerSourceTypeCamera; [self presentViewController:ipc animated:YES completion:nil]; } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Desc" message:@"Camera capture is not supported in this device" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; [alert release]; } 

如何解决这个问题?

当我select从相机捕捉时,它崩溃。 它不会从上面的代码崩溃,但之后崩溃与下面的错误。

我得到这个错误:

由于未捕获的exception“UIApplicationInvalidInterfaceOrientation”,终止应用程序,原因:“支持的方向与应用程序没有共同的方向,并且shouldAutorotate返回YES

而我的应用程序崩溃。

我在这个视图上的定位代码。

 -(BOOL)shouldAutorotate { return YES; } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight ; } 

我以前也遇到同样的问题。 我遵循了这些步骤。 请尝试这个,让我知道如果你面临同样的问题。 复选标记 在这里输入图像说明

步骤1:检查appdelegate.m

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

第2步:在你的视图控制器

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft || interfaceOrientation==UIInterfaceOrientationLandscapeRight) return YES; return NO; } 

第3步:你的视图控制器

 -(IBAction)TakePhoto:(id)sender{ if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { UIImagePickerController* imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.allowsEditing = YES; imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage, nil]; [self.view.window.rootViewController presentViewController:imagePicker animated:YES completion:nil];//add to view as per requirement } else { UIAlertView *noCam = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"There is No Camera Fecility" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [noCam show]; } } 

在启动UIImagePickerController或者在使用相机拍摄图像之后,会发生崩溃吗? 我在运行iOS7的iPod上试用了你的代码,并且工作正常。 这个问题可能在别的地方。 由于内存使用情况,我已经看到UIImagePickerController发生崩溃,所以可能是你要检查的东西。 此外,虽然我们在它, presentModalViewController:animated:自iOS6.0已弃用。 你需要使用presentViewController:animated:completion:来代替。 也看到你的UIAlertView发布声明,它看起来像你没有使用ARC所以内存使用情况绝对是我会看。 希望这可以帮助。

编辑:从UIImagePickerController文档
Important: The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified, with one exception. You can assign a custom view to the cameraOverlayView property and use that view to present additional information or manage the interactions between the camera interface and your code.

试试这个代码,它适用于我的旧应用程序。

 -(BOOL)shouldAutorotate { return YES; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; } 

可能是你想检查: 横向专用的应用程序中的GameCenter身份validation将引发UIApplicationInvalidInterfaceOrientation

我发现我的解决scheme从链接iOS7的iPad景观只有应用程序,使用UIImagePickerController 。

它对我来说就像一个魅力。

希望它也帮助别人。

感谢您的帮助人。

我已经inheritance了UIImagePickerController,并重写拖动方法到支持的风景(或者你可以做一个类别):

 - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; } - (BOOL)shouldAutorotate { return YES; } 

然后在支持的界面方向(iPad)中添加纵向(底部主页button),横向(左侧主页button),横向(右侧主页button)。 支持的界面方向(iPad)

在这里,必须添加Portrait(底部主页button)值,因为UIImagePickerController只支持纵向模式,所以我们需要添加纵向模式,否则会引发exception。