SpriteKit SKTexture.preloadTextures high memory usage Swift

I have an SKTextureAtlas with about 90 PNG images. Each image has a resolution of 2000 x 70 pixels and is ~ 1KB in size.

Now I was putting these images from 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 textures in the didMoveToView file:

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

      

To play the animation at 30 fps , I use SKAction.animateWithTextures

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

      

My problem is that when preloading textures, the memory usage goes up to about 300MB. Is there a better solution?
And what is the recommended frame rate and image size for SKAction.animateWithTextures?

0


source to share


1 answer


You have to keep in mind that the size of the image file (1Kb in your example) has nothing to do with the amount of memory required to keep the same image in RAM. You can calculate the amount of memory required using this formula:

width x height x bytes per pixel = size in memory



If you are using the standard RGBA8888 format, this means that your image will require about 0.5 megabytes of RAM, since RGBA8888 uses 4 bytes per pixel - 1 byte for each red, green, blue and 1 byte for alpha transparency. You can read it here .

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

+2


source







All Articles