菜鸟教程小白 发表于 2022-12-11 19:49:34

c# - Unity iOS 应用图像缓存问题


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

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

<pre><code>public class SpriteCache : Singleton&lt;SpriteCache&gt;
{
    Dictionary&lt;string, Sprite&gt; _cache = new Dictionary&lt;string, Sprite&gt;();

    public void LoadSprite(string url, Action&lt;Sprite&gt; callback)
    {
      StartCoroutine(LoadSpriteCoroutine(url, callback));
    }

    public IEnumerator LoadSpriteCoroutine(string url, Action&lt;Sprite&gt; callback)
    {
    if (_cache.ContainsKey(url))
    {
      callback(_cache);
      yield break;
    }

    var www = new WWW(url);

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

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

    var texture = www.texture;
    if (texture == null)
    {
      Debug.LogErrorFormat(&#34;No texture found at &#39;{0}&#39;!&#34;, 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 = sprite;
    callback(sprite);
}
</code></pre>

<p>编辑:</p>

<p>进一步解释澄清</p>

<pre><code>var www = new WWW(url)
</code></pre>

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

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

<pre><code>var www = WWW.LoadFromCacheOrDownload(url, 1)
</code></pre>

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

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

<p>如果在 _cache 中,则从 _cache 加载图像:</p>

<pre><code>if (_cache.ContainsKey(url))
{
    callback(_cache);
    yield break;
}
</code></pre>

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

<pre><code>var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(texture.width / 2, texture.height / 2));
_cache = sprite;
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>所以我想出了一个解决方案,</p>

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

<p>这个保存功能对我有用:</p>

<pre><code>//if file doesn&#39;t exist then save it for fututre reference
    if (!File.Exists(_FileLocation + texName))
    {
      byte[] bytes;

      if (Path.GetExtension(texName) == &#34;.png&#34;)
      {
            bytes = texture.EncodeToPNG();
      }
      else
      {
            bytes = texture.EncodeToJPG();
      }
      File.WriteAllBytes(Application.persistentDataPath + texName, bytes);
    }
</code></pre>

<p>该部分放置在“callback(sprite)”之后。</p>

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

<pre><code> texName = Path.GetFileName(texName);

    if (File.Exists( _FileLocation + texName))
    {
      url = _FileLocation + &#34;/&#34;+ texName;
    }
</code></pre>

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

<p>_FileLocation 定义如下</p>

<pre><code>string _FileLocation;

public void Start()
{
    _FileLocation = Application.persistentDataPath;
}
</code></pre>

<p>在该方法中引用它的原因是通过应用程序调用地址来节省性能,如果您正在使用跨平台解决方案,那么可能需要更改保存的数据路径,因此您可以进行检测,如果或 case 语句来更改 _FileLocation 以满足您的需要。</p></p>
                                   
                                                <p style="font-size: 20px;">关于c# - Unity iOS 应用图像缓存问题,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/49115571/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/49115571/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: c# - Unity iOS 应用图像缓存问题