How do I get rid of SKTextures?
-
21-12-2019 - |
Question
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?
Solution
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.
OTHER TIPS
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:
- create UIImages with +imageWithContentsOfFile:
- create SKTextures from the resulting UIImage objects by calling SKTexture/+textureWithImage:(UIImage*).
Pulled from this answer: iOS 7 Sprite Kit freeing up memory