iOS 7 Sprite Kit释放内存 [英] iOS 7 Sprite Kit freeing up memory

查看:98
本文介绍了iOS 7 Sprite Kit释放内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一款针对新iOS 7和Sprite Kit的iOS游戏,使用发射器节点和物理来增强游戏性。在开发应用程序时,我遇到了一个严重的问题:你创建场景,节点,效果,但是当你完成并需要返回到主屏幕时,你如何释放这些资源分配的所有内存?

I am building an iOS game aimed for the new iOS 7 and Sprite Kit, using emitter nodes and physics to enhance gameplay. While developing the app, I ran into a serious problem: you create your scenes, nodes, effects, but when you are done and need to return to the main screen, how do you free up all the memory allocated by these resources?

理想情况下,ARC应该释放所有内容,应用程序应该恢复到创建场景之前的内存消耗级别,但事实并非如此。

Ideally ARC should free up everything and the application should get back to the memory consumption level it had before creating the scene, but this is not what happens.

我添加了以下代码,作为视图的dealloc方法,它绘制场景并负责在关闭(删除)后删除所有内容:

I've added the following code, as the dealloc method of the view, which draws the scene and is responsible for removing everything upon getting closed (removed):

- (void) dealloc
{
    if (scene != nil)
    {
        [scene setPaused:YES];

        [scene removeAllActions];
        [scene removeAllChildren];

        scene = nil;

        [((SKView *)sceneView) presentScene:nil];

        sceneView = nil;
    }
}




  • sceneView是一个UIView ,这是场景的容器

  • 场景是SKScene类的扩展,创建所有SKSpriteNode对象

  • 我非常感谢你对此事的任何帮助。

    I would very much appreciate any help on this matter.

    推荐答案

    我有很多内存问题Sprite Kit,我使用技术支持票来获取信息,这可能与此有关。我问的是,开始一个新的SKScene是否会完全释放前一个使用的所有内存。我发现了这个:

    I had a lot of memory problems with Sprite Kit, and I used a tech support ticket to get info, and it may relate here. I was asking whether starting a new SKScene would totally release all the memory the previous one used. I found out this:

    + textureWithImageNamed分配的底层内存:切换到新的SKScene时可能会或可能不会(通常不会)释放。你不能依赖它。 iOS释放由+ textureWithImageNamed:或+ imageNamed:缓存的内存,当它看起来合适时,例如当它检测到低内存条件时。

    The underlying memory allocated by +textureWithImageNamed: may or may not (typically not) be released when switching to new SKScene. You cannot rely on that. iOS releases the memory cached by +textureWithImageNamed: or +imageNamed: when it sees fit, for instance when it detects a low-memory condition.

    如果你想在完成纹理后立即释放内存,你必须避免使用+ textureWithImageNamed:/ + imageNamed:。创建SKTextures的另一种方法是:首先用+ imageWithContentsOfFile:创建UIImages,然后通过调用SKTexture / + textureWithImage :( UIImage *)从生成的UIImage对象创建SKTextures。

    If you want the memory released as soon as you’re done with the textures, you must avoid using +textureWithImageNamed:/+imageNamed:. An alternative to create SKTextures is to: first create UIImages with +imageWithContentsOfFile:, and then create SKTextures from the resulting UIImage objects by calling SKTexture/+textureWithImage:(UIImage*).

    我不知道这是否有帮助。

    I don't know if this helps here.

    这篇关于iOS 7 Sprite Kit释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆