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

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

问题描述

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

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

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

- (void) dealloc{如果(场景!= nil){[场景设置暂停:是];[场景removeAllActions];[场景removeAllChildren];场景 = 无;[((SKView *)sceneView)presentScene:nil];场景视图 = 零;}}

  • sceneView 是一个 UIView,是场景的容器
  • scene 是 SKScene 类的扩展,创建了所有 SKSpriteNode 对象

我非常感谢您对此事的任何帮助.

解决方案

我在使用 Sprite Kit 时遇到了很多内存问题,我使用了技术支持票来获取信息,这可能与此处有关.我在问启动一个新的 SKScene 是否会完全释放前一个使用的所有内存.我发现了这一点:

+textureWithImageNamed: 分配的底层内存可能会或可能不会(通常不会)在切换到新的 SKScene 时释放.你不能依赖那个.iOS 在认为合适时释放由 +textureWithImageNamed: 或 +imageNamed: 缓存的内存,例如当它检测到内存不足的情况时.

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

我不知道这对这里是否有帮助.

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?

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.

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 is a UIView, which is the container of the scene
  • scene is an extension of the SKScene class, creating all the SKSpriteNode objects

I would very much appreciate any help on this matter.

解决方案

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:

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.

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天全站免登陆