SpriteKit SKTexture.preloadTextures high memory usage Swift

狂风中的少年 提交于 2019-11-28 13:09:56

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/29109445/spritekit-sktexture-preloadtextures-high-memory-usage-swift

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