做了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;
}