Jonas Ester
Jonas Ester

Reputation: 505

SpriteKit SKTexture.preloadTextures high memory usage Swift

I have a SKTextureAtlas with about 90 PNG Images. Every Image has a resolution of 2000 x 70 pixel and has a size of ~1 KB.

Now I put this images from the Atlas into an array like this:

var dropBarAtlas = SKTextureAtlas(named: "DropBar")

for i in 0..<dropBarAtlas.textureNames.count{
        var textuteName = NSString(format: "DropBar%i", i)
        var texture = dropBarAtlas.textureNamed(textuteName)
        dropFrames.addObject(texture)
   }

Then I preload the array with the textures in didMoveToView:

SKTexture.preloadTextures(dropFrames, withCompletionHandler: { () -> Void in})

To play the animation with 30 fps I use SKAction.animateWithTextures

var animateDropBar = SKAction.animateWithTextures(dropFrames, timePerFrame: 0.033)
dropBar.runAction(animateDropBar)

My problem is that when I preload the textures the memory usage increases to about 300 MB. Is there a more performant solution?
And which frame rate and image size is recommended for SKAction.animateWithTextures?

Upvotes: 0

Views: 1275

Answers (1)

Whirlwind
Whirlwind

Reputation: 13675

You should keep in mind that image file size (1Kb in your example) have nothing with amount of memory required for same image to be stored in RAM . You can calculate that amount of memory required with this formula:

width x height x bytes per pixel = size in memory

If you using standard RGBA8888 pixel format this means that your image will require about 0.5 megabytes in RAM memory, because RGBA8888 uses 4 bytes per pixel – 1 byte for each red, green, blue, and 1 byte for alpha transparency. You can read more here.

So what you can do, is to optimize you textures and use different texture formats. Here is another example about texture optimization.

Upvotes: 2

Related Questions