0

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?

Jonas Ester
  • 535
  • 4
  • 19
  • The problem is that you simply cannot decode that many images into main memory at the same time. There are more advanced animations available that do not hold all the decoded bytes in memory at once. – MoDJ Apr 02 '16 at 01:48
  • For an example of effective SpriteKit texture compression, see this answer http://stackoverflow.com/a/38679128/763355 – MoDJ Aug 05 '16 at 19:48

1 Answers1

2

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.

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • 1
    For 2016+ readers, you should check out the WWDC 2015 videos on SpriteKit and GameplayKit. Short version: Xcode texture atlases already perform a lot of optimisations on your images, and iOS 9's app slicing technology means that a user of your app will automatically receive only those graphic resources that the device needs. e.g. an iPhone 5s user will only download X2 resolution images, not X1 or X3. Also, iOS performs automatic caching of textures once they are used. (Though there are methods to preload textures as well.) – Womble Mar 20 '16 at 01:46