cocos2dx ios中横屏切换竖屏相册崩溃

做了3个月的项目,终于把安卓版本弄上线了,接下来就是苦逼的搞苹果版本。然而一开始就遇到一个问题,因为我们项目是横屏的,但是调用系统相册的时候,因为系统相册是竖屏的,直接就闪退了。

报错信息:'Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES'


百度了一下,看来很有很多童鞋也遇到过类似的问题。看了几篇都说的大同小异,自己根据他们的方法试了一下也没有解决问题,算是走了点小弯路,所以记录一下。


一开始还以为要在info.plist里面把Protrait加上,导致的结果是进去的时候取到的屏幕尺寸是640*1136。然后屏幕适配也有问题。果断放弃。后来看到别人的帖子里面有提到supportedInterfaceOrientationsForWindow这个方法,不过人家是在AppDelegate.mm里面加的,而我们项目用的是cocos2dx版本,所以我加在了AppController.mm中。

如下:

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

//    NSLog(@"supportedInterfaceOrientationsForWindow..");

    return UIInterfaceOrientationMaskAll;

}


此代码表示项目支持全部的方向。

然而我们项目实际在运行时其实一般都是横屏的,所以在RootViewController.mm中修改


// For ios6, use supportedInterfaceOrientations & shouldAutorotate instead

- (NSUInteger) supportedInterfaceOrientations{    

#ifdef __IPHONE_6_0

    return UIInterfaceOrientationMaskLandscape;

#endif    

}

此代码表示游戏内仅支持横屏


然后在我自己调用系统相册的ImagePickerViewController.mm文件中重写函数


-(NSUInteger)supportedInterfaceOrientations

{

    NSLog(@"image....supportedInterfaceOrientations..inPhoto:%d",inPhoto);

    if (inPhoto) {

        return UIInterfaceOrientationMaskPortrait;

    }

    return UIInterfaceOrientationMaskLandscape;

    

}


inPhoto为bool值,在进入相册或相机前设置为true,这样进入的时候会自动切为竖屏。在取消照片或拍照或成功选中照片时设置为false,这样相册或相机退出时,游戏会自动在切为横屏。这样就解决问题啦。其实解决的思路还是挺简单的,主要是对ios不熟,遇到问题也就只能这样一步一步尝试过来


最后记得要支持屏幕旋转哦。


- (BOOL) shouldAutorotate {

    return YES;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值