【问题标题】:Orientation issue in Landscape mode while opening camera in iOS 7 in iPhone在 iPhone 的 iOS 7 中打开相机时横向模式下的方向问题
【发布时间】:2014-05-21 04:40:11
【问题描述】:

我有一个仅处于横向模式的应用程序。在我的应用程序中,我从我的一个视图中打开相机。它在我的 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'而终止应用程序,原因:'支持的方向与应用程序没有共同的方向,并且 shouldAutorotate 正在返回 YES

我的应用程序崩溃了。

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

-(BOOL)shouldAutorotate
{
    return YES;
}

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

【问题讨论】:

    标签: ios iphone objective-c ios7 uiimagepickercontroller


    【解决方案1】:

    我以前也遇到过同样的问题。我按照步骤操作。请试试这个,让我知道你是否面临同样的问题。 复选标记

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

    【讨论】:

    • 我用上面提到的链接解决了我的问题。你的代码有问题吗?那我可以帮你。
    【解决方案2】:

    崩溃是在启动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.

    【讨论】:

    • 我已经编辑了我的答案。选择器仅支持肖像,而您的应用不支持。
    • 您可以做的是创建一个 UIView 并将 UIImagePickerView 作为子视图添加到它。有几篇文章展示了如何做到这一点,或者您可以使用此处描述的 AVFoundation 框架:stackoverflow.com/a/11232352/1113598
    • 是的,没错,但问题是我给了我的应用程序所有支持的方向,并将方向锁定为仅用于相机视图的纵向,并且它工作正常,但对于我的第一个视图,它仅以纵向显示模式。所有视图都被锁定,但只有我的第一个 VC 以纵向模式显示。
    • stackoverflow.com/questions/20468335/… 看看这个问题,你能告诉我我需要从我的项目中创建哪个类别吗?
    • 您需要执行第 1 步。在第 2 步中,您需要检查支持的方向并返回它们。第 3 步将按原样添加,因为您需要选择器以横向方式启动。
    【解决方案3】:

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

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

    您可能想检查一下:GameCenter authentication in landscape-only app throws UIApplicationInvalidInterfaceOrientation

    【讨论】:

      【解决方案4】:

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

      它对我很有用。

      希望它也对其他人有所帮助。

      感谢您的帮助。

      【讨论】:

        【解决方案5】:

        我继承了 UIImagePickerController,并覆盖了两个方法来支持景观(或者你可以创建一个类别):

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

        并在支持的界面方向(iPad)中添加纵向(底部主页按钮)、横向(左侧主页按钮)、横向(右侧主页按钮)。

        这里必须添加Portrait(底部home键)的值,因为UIImagePickerController只支持竖屏模式,所以我们需要添加竖屏模式,否则会抛出异常。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-04-21
          • 1970-01-01
          • 1970-01-01
          • 2014-07-07
          • 1970-01-01
          相关资源
          最近更新 更多