OGeek|极客世界-中国程序员成长平台

标题: c# - Unity iOS 应用图像缓存问题 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:49
标题: c# - Unity iOS 应用图像缓存问题

我已经接管了一个应用程序的开发,该应用程序已完成减去需要 Internet 连接来加载图像的错误,因为尽管尝试这样做,它仍无法从缓存中访问它们。

谁能帮我弄清楚以下问题出在哪里?

public class SpriteCache : Singleton<SpriteCache>
{
    Dictionary<string, Sprite> _cache = new Dictionary<string, Sprite>();

    public void LoadSprite(string url, Action<Sprite> callback)
    {
        StartCoroutine(LoadSpriteCoroutine(url, callback));
    }

    public IEnumerator LoadSpriteCoroutine(string url, Action<Sprite> callback)
    {
    if (_cache.ContainsKey(url))
    {
        callback(_cache[url]);
        yield break;
    }

    var www = new WWW(url);

    while (!www.isDone)
    {
        yield return www;
    }

    if (!string.IsNullOrEmpty(www.error))
    {
        Debug.LogErrorFormat("Tried to obtain texture at '{0}' but received error '{1}'", url, www.error);
        yield break;
    }

    var texture = www.texture;
    if (texture == null)
    {
        Debug.LogErrorFormat("No texture found at '{0}'!", url);
        yield break;
    }

    var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(texture.width / 2, texture.height / 2));
    _cache[url] = sprite;
    callback(sprite);
}

编辑:

进一步解释澄清

var www = new WWW(url)

这会抓取存储在可以工作的服务器上的图像,据我所知,在抓取图像的一个实例之后,这应该将项目放入缓存中以供以后使用。

我已经尝试使用以下更新的方法来查看是否可以解决问题。

var www = WWW.LoadFromCacheOrDownload(url, 1)

这导致它无法以任何身份工作,并且永远不会更改占位符中的图像。

“LoadSpriteCoroutine”中的第一个 if 语句应该通过检查是否存在 url 的键来捕获 Sprite 是否已经存在于“_cache”字典中,该键应该在第一个运行实例和 internet 之后连接

如果在 _cache 中,则从 _cache 加载图像:

if (_cache.ContainsKey(url))
{
    callback(_cache[url]);
    yield break;
}

如果之前没有图像,则将图像添加到 _cache:

var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(texture.width / 2, texture.height / 2));
_cache[url] = sprite;



Best Answer-推荐答案


所以我想出了一个解决方案,

所有其他将图像保存到缓存的方法在 iOS 上似乎都失败了,我删除了所有以前的“_cache”内容。

这个保存功能对我有用:

//if file doesn't exist then save it for fututre reference
    if (!File.Exists(_FileLocation + texName))
    {
        byte[] bytes;

        if (Path.GetExtension(texName) == ".png")
        {
            bytes = texture.EncodeToPNG();
        }
        else
        {
            bytes = texture.EncodeToJPG();
        }
        File.WriteAllBytes(Application.persistentDataPath + texName, bytes);
    }

该部分放置在“callback(sprite)”之后。

对已保存项目的检查放置在旧的“if(_cache.ContainsKey(url))”部分所在的位置

 texName = Path.GetFileName(texName);

    if (File.Exists( _FileLocation + texName))
    {
        url = _FileLocation + "/"+ texName;
    }

请注意,“LoadSpriteCoroutine”现在在其调用中采用了一个额外的字符串参数“texName”,它只是 url 的一个缩短实例,然后它会将其剪切为文件名和扩展名。如果找到匹配项,则将 url 替换为持久文件路径 + 文件名,以便在本地获取它,而不是像往常一样通过网络获取。

_FileLocation 定义如下

string _FileLocation;

public void Start()
{
    _FileLocation = Application.persistentDataPath;
}

在该方法中引用它的原因是通过应用程序调用地址来节省性能,如果您正在使用跨平台解决方案,那么可能需要更改保存的数据路径,因此您可以进行检测,如果或 case 语句来更改 _FileLocation 以满足您的需要。

关于c# - Unity iOS 应用图像缓存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49115571/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4