初学任何一个东西都会让人感觉疲惫。
索性这次还没有用Objective-C而是用C++来开发的cocos-2dx项目。

我想把这次遇到的一些问题记录一下。

问题一,如何把ios项目上传到appstore

一个iOS项目,如果不设置任何的开发者账号,连在手机上测试都做不到。
必须要绑定了开发这账号以后,才能在手机上测试,即便如此,也无法打包交给别人测试(可以,但是很麻烦),需要绑定某个特定手机的udid。

苹果开发者账号分三种,分别是个人、公司(¥688)以及企业($299),企业相比前两者多了不经过苹果审批直接给其他人使用的功能。

工程在经过打包后,即可上传,或者交给别人。

问题二,如何设置强制竖屏。

在RootViewController.mm文件中,修改这几个方法。能够在代码上修改软件行为。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Override to allow orientations other than the default portrait orientation.
// This method is deprecated on ios6
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}

// For ios6, use supportedInterfaceOrientations & shouldAutorotate instead
- (NSUInteger) supportedInterfaceOrientations{
#ifdef __IPHONE_6_0
return UIInterfaceOrientationMaskPortrait;
#endif
}

- (BOOL) shouldAutorotate {
return NO;
}

但是除此之外,还需要在项目配置中,对项目进行设置。

如图

问题三,打包的时候,一个错误。

Enable bitcode。

bitcode是在ios9上的一个新特性,可以针对某个系统进行打包(具体原理没有仔细研究),能够减小打包的大小。但是很多第三方的包还不太支持这个特性,所以就会报错需要在项目配置中就行禁用。搜索Enable bitcode,选择No。该问题得到解决。

问题四,首页Splash图片和icon

我不知道native的ios项目中是在哪里的,但是cocos-2dx生成的项目中,这两种图片是在中ios文件夹icon目录中的,其中有文件Info.plist作为配置文件,定义了用哪些图片,如何用。

这些图片我是使用python的pillow(PIL)库,切出来的。

列代码如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

def open(fp):
im = Image.open(fp)

print(im.format, im.size, im.mode)
return im


def save(im, fout="default.png"):
try:
im.save(fout, "PNG", quality=95)
except IOError:
print("cannot create file")


def resize(im, width, height, fout="default.png"):
out = im.resize((width, height), Image.ANTIALIAS)
save(out, fout)


/* ======file main.py====== */
import image


def _resize(fin, _size=None):
im = image.open(fin)
for _s in _size:
image.resize(im, _s, _s, fout="Icon-{}.png".format(_s))


def _resize2(fin, _w, _h, fout):
im = image.open(fin)
image.resize(im, _w, _h, fout)

if __name__ == '__main__':
_resize2("loading.png", 2208, 1242, "Default-736h@3x.png")

问题五,一个诡异的内存问题

在项目中遇到了字符串打不出来的情况,而且是有时候能够显示,有时候不能显示,有时候是乱码,莫名其妙,不可理喻。

经过长时间的端点跟踪,我们发现。

1
2
3
4
5
6
void EnsureLayer::show_ensure(
const char* title,
const ccMenuCallback& callback)
{
...
}

通过调用该方法,在屏幕上增加一个对话框。在在这个方法外面打log的时候,title打印出来是正确的,而在这个方法里面打log时,就出现问题了,title的地址没有变,但是指向的内容却可能不对,这证明title指向的内存被回收了,但是为什么会被回收,以及在什么时候被回收的,到最后也不知道。

最后我们决定在函数内部,访问该字符串,解决了这个问题,但是上面这个疑问还是没有释怀,现在依旧不知道。