Texture2d.SaveAsPng()内存泄漏 [英] Texture2d.SaveAsPng() Memory Leak

查看:134
本文介绍了Texture2d.SaveAsPng()内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现方法

I have found strange issue with method Texture2d.SaveAsPng() Every call 1.5mb disapear. I use this method to save texture to isolated storage

public static void SaveTextureToISF(string fileName, Texture2D texture)
        {
            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (
                    IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(fileName, FileMode.Create, file)
                    )
                {
                    texture.SaveAsPng(fileStream, texture.Width, texture.Height);
                    fileStream.Close();
                }
            }
        }

我需要保存大量纹理,并且内存泄漏很大. 在Windows Phone 8设备上一切正常,仅在Windows Phone 7上存在此问题.

I need to save large amount of textures and i have huge memory leak. On windows phone 8 devices all works fine, this issue only on windows phone 7.

推荐答案

通过在byte数组中获取纹理数据并将其保存到隔离存储,解决了该问题.

Solved problem by getting texture data in byte array and saving it to Isolated Storage.

public static void SaveTextureToISF(string fileName, Texture2D texture)
{
    byte[] textureData = new byte[4 * texture.Width * texture.Height];
    texture.GetData(textureData);
    Save(fileName, textureData); //saving array to IS
}

然后,当需要纹理时,从存储中加载byte数组,并将此数据加载到新纹理中.

And when texture is needed, load byte array from storage and load this data to new texture.

 public static Texture2D LoadTextureFromISF(string fileName, int width, int height)
 {
     Texture2D texture = new Texture2D(GraphicsDevice, width, height);
     byte[] textureData = Load(fileName); //load array from IS
     texture.SetData(textureData);
     return texture;
 }

需要注意的一件事,当从存储中加载纹理时,您应该确切地知道已保存纹理的尺寸并将其作为参数传递到加载函数中.可以很容易地修改它,但是我不需要.

One thing to note, when loading texture from storage you should know exactly dimension of saved texture and pass it as parameters in load function. This can be easily modified, but I don't need to.

这篇关于Texture2d.SaveAsPng()内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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