SpriteKit SKTexture.preloadTextures高内存使用率Swift [英] SpriteKit SKTexture.preloadTextures high memory usage Swift

查看:118
本文介绍了SpriteKit SKTexture.preloadTextures高内存使用率Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有约90个PNG图像的SKTextureAtlas.每个图像的分辨率为2000 x 70像素,大小约为1 KB.

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.

现在,我将来自Atlas的图像放入这样的数组中:

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)
   }

然后我在didMoveToView中将纹理预加载到数组中

Then I preload the array with the textures in didMoveToView:

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

要以 30 fps播放动画,我使用SKAction.animateWithTextures

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

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

我的问题是,当我预加载纹理时,内存使用量增加到约300 MB. 有更高效的解决方案吗?
对于SKAction.animateWithTextures,建议使用哪种帧速率和图像尺寸?

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?

推荐答案

您应该记住,图像文件大小(在您的示例中为1Kb)没有任何容量,而将同一图像存储在RAM中所需的内存量也没有.您可以使用以下公式计算所需的内存量:

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:

宽度x高度x每个像素的字节=内存大小

width x height x bytes per pixel = size in memory

如果您使用标准RGBA8888像素格式,这意味着您的图像将需要约0.5 MB的RAM内存,因为RGBA8888每像素使用4个字节-红色,绿色,蓝色每个像素1个字节,alpha透明度每个字节1个字节.您可以阅读更多的这里.

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.

这篇关于SpriteKit SKTexture.preloadTextures高内存使用率Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆