How do I get rid of SKTextures?

吃可爱长大的小学妹 提交于 2019-12-11 08:35:07

问题


Maybe this is easy, but I'm lost.

I have a project where I have to make a full-screen animation that uses 8 jpgs to simulate a page opening. So what I am doing is:

  • I have an SKSpriteNode that displays on the full screen
  • making an array of 8 SKTextures
  • using SKTexture preloadTextures to load them
  • once they're loaded, I use animateWithTextures to show the animation
  • later on, another method removes the SKSpriteNode from the scene.

When I first do the page turn, it uses a ton of memory, but when I run removeFromParent on it, the memory continues to be used.

My .m file declares this at the top:

SKSpriteNode *pageTurnNode;

because I want to be able to refer to it easily in both methods.

How do I get rid of all those textures and whatnot?


回答1:


Textures may not be released from memory immediately. Apparantely Sprite Kit employs a caching system. It will remove cached textures when it sees fit.

That and what @prototypical said.




回答2:


This seems to be pretty valuable and relates to this question, I feel.

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:

  1. create UIImages with +imageWithContentsOfFile:
  2. create SKTextures from the resulting UIImage objects by calling SKTexture/+textureWithImage:(UIImage*).

Pulled from this answer: iOS 7 Sprite Kit freeing up memory



来源:https://stackoverflow.com/questions/21025295/how-do-i-get-rid-of-sktextures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!