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

查看:13
本文介绍了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 兆字节的 RAM 内存,因为 RGBA8888 每个像素使用 4 个字节 - 每个红色、绿色、蓝色各 1 个字节,以及 1 个字节用于 alpha 透明度.您可以阅读更多 这里.

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天全站免登陆