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

时间:2014-05-21 04:40:12

标签: ios iphone objective-c ios7 uiimagepickercontroller

我有一个仅在横向模式下的应用程序。在我的应用程序中,我从我的一个视图中打开相机。它适用于我的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];
}

如何解决这个问题?

当我选择从相机捕捉时它会崩溃。它不会从上面的代码中崩溃,但在此之后崩溃时会出现以下错误。

我收到此错误:

  

由于未捕获的异常而终止应用程序' UIApplicationInvalidInterfaceOrientation',原因:'支持的方向与应用程序没有共同的方向,并且应该是返回YES

我的应用程序崩溃了。

此视图上的我的方向代码。

-(BOOL)shouldAutorotate
{
    return YES;
}

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

5 个答案:

答案 0 :(得分:2)

我以前也遇到过同样的问题。我按照步骤。如果您遇到同样的问题,请试试这个并告诉我。 选中标记enter image description here

第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];
    }
}

答案 1 :(得分:1)

在启动UIImagePickerController或使用相机拍摄图像后是否发生了崩溃?我在运行iOS7的iPod上试用了你的代码,它运行正常。问题可能出在其他地方。由于内存使用情况,我发现UIImagePickerController发生了崩溃,因此您可以查看。此外,在我们处理它的同时,自{i }.0以来,presentModalViewController:animated:已被弃用。您需要使用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.

答案 2 :(得分:0)

尝试使用此代码,它适用于我的旧应用。

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

您可能想要查看此内容:GameCenter authentication in landscape-only app throws UIApplicationInvalidInterfaceOrientation

答案 3 :(得分:0)

我从链接iOS7 iPad Landscape only app, using UIImagePickerController找到了我的解决方案。

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

希望它也帮助别人。

感谢您的帮助。

答案 4 :(得分:0)

我继承了UIImagePickerController,并将两种方法覆盖到支持的格局(或者你可以创建一个类别):

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (BOOL)shouldAutorotate
{
    return YES;
}

在支持的界面方向(iPad)中添加纵向(底部主页按钮),横向(左主页按钮),横向(右主页按钮)。 Supported interface orientations(iPad)

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