亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

加載圖像表單 PersistenceDataPath

加載圖像表單 PersistenceDataPath

C#
SMILET 2022-11-13 17:28:17
我正在制作一個 Android 應用程序,它應該在開始時從 MySQL 數據庫加載數據和從服務器加載一些圖像。之后,用戶將在外面,因此應用程序將離線工作。我編寫了代碼以將所有數據庫數據(有效)和圖像從服務器保存到 ApplicationPersistenceDataPath(無效)。我已經在尋找我應該怎么做。我的代碼不會引發任何錯誤。但是,當我嘗試在應用程序上顯示它時,圖像是空白的。那是當我查看存儲圖像的文件夾時,我無法打開它們。這是我必須保存圖像的方法:IEnumerator loadBgImage(string url, string file_name)    {        using (UnityWebRequest www = UnityWebRequest.Get(url))        {            yield return www.Send();            if (www.isNetworkError || www.isHttpError)            {                print("erro");            }            else            {                string savePath = string.Format("{0}/{1}", Application.persistentDataPath, file_name);                if (!File.Exists(savePath))                {                    System.IO.File.WriteAllText(savePath, www.downloadHandler.text);                }            }        }    }這是在圖像中顯示圖像文件的代碼:string path = Application.persistentDataPath + "/" + nomeImagem;imagem.GetComponent<SpriteRenderer>().sprite = LoadSprite(path);這是稱為的方法:    private Sprite LoadSprite(string path)    {        if (string.IsNullOrEmpty(path)) return null;        if (System.IO.File.Exists(path))        {                        byte[] bytes = File.ReadAllBytes(path);            Texture2D texture = new Texture2D(900, 900, TextureFormat.RGB24, false);            texture.filterMode = FilterMode.Trilinear;            texture.LoadImage(bytes);            Sprite sprite = Sprite.Create(texture, new Rect(0, 0, 8, 8), new Vector2(0.5f, 0.0f), 1.0f);        }        return null;    }我真的需要先保存圖像,然后離線顯示。有人可以告訴我我做錯了什么嗎?
查看完整描述

1 回答

?
炎炎設計

TA貢獻1808條經驗 獲得超4個贊

一般來說,你應該寧愿使用Path.Combinelike


var path = Path.Combine(Application.persistentDataPath, FileName);

代替系統路徑!可能是您的目標設備根本不理解/為路徑分隔符。


如果文件已經存在(也許是更新的版本?而且你已經下載了它)也可以寫,或者如果它已經存在你甚至不應該開始下載以節省帶寬。


我還認為這WriteAllText不是二進制圖像數據的正確解決方案,因為www.downloadHandler.text已經


解釋為 UTF8 字符串


因此,由于圖像很可能有一些字節在 UTF8 字符串中無法表示,因此您會在此處獲得損壞的數據!


你寧愿直接使用


www.downloadHandler.data

它返回原始byte[]而不是例如File.WriteAllBytes(僅在UWP上)或將其FileStream寫入byte[]文件的正確方法。就像是


var data = www.downloadHandler.data;

File.WriteAllBytes(path, data);

或者


var data = www.downloadHandler.data;

using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write))

{

    fs.Write(data, 0, data.Length);

}

比場景中的白色圖像通常意味著精靈是null.


如果沒有打字錯誤,那么您LoadSprite 總是returns null。你忘了歸還創建的sprite


private Sprite LoadSprite(string path)

{

    if (string.IsNullOrEmpty(path)) return null;

    if (System.IO.File.Exists(path))

    {            

        byte[] bytes = File.ReadAllBytes(path);

        Texture2D texture = new Texture2D(900, 900, TextureFormat.RGB24, false);

        texture.filterMode = FilterMode.Trilinear;

        texture.LoadImage(bytes);

        Sprite sprite = Sprite.Create(texture, new Rect(0, 0, 8, 8), new Vector2(0.5f, 0.0f), 1.0f);


        // You should return the sprite here!

        return sprite;

    }

    return null;

}

但是請注意,加載本地圖像的更好方法實際上UnityWebRequestTexture也是如此。它還將本地文件路徑作為 URL 和


與下載原始字節并在腳本中手動創建紋理相比,使用此類可顯著減少內存重新分配。此外,紋理轉換將在工作線程上執行。


但是也


僅支持 JPG 和 PNG 格式。


但這不應該是我想的問題,因為LoadImage到目前為止您使用的也是相同限制的基礎。


但是,這將是異步的,因此您必須等待紋理才能創建精靈。因此,與其重新調整,Sprite我實際上寧愿傳入相應Image組件的引用并直接替換其sprite. 就像是


private IEnumerator LoadLocalTexture(string path, Image receivingImage)

{

    UnityWebRequest www = UnityWebRequestTexture.GetTexture(path);

    yield return www.SendWebRequest();


    if(www.isNetworkError || www.isHttpError) 

    {

        Debug.Log(www.error);

    }

    else 

    {

        var texture = ((DownloadHandlerTexture)www.downloadHandler).texture;

        var sprite = Sprite.Create(texture, new Rect(0, 0, 8, 8), new Vector2(0.5f, 0.0f), 1.0f);


        receivingImage.sprite = sprite;

    }

}

如果您實際上并不一定要使用,Sprites我總是建議您RawImage改用可以直接使用的組件Texture2D!而不是做類似的事情


private IEnumerator LoadLocalTexture(string path, RawImage receivingImage)

{

    UnityWebRequest www = UnityWebRequestTexture.GetTexture(path);

    yield return www.SendWebRequest();


    if(www.isNetworkError || www.isHttpError) 

    {

        Debug.Log(www.error);

    }

    else 

    {

        var texture = ((DownloadHandlerTexture)www.downloadHandler).texture;


        receivingImage.texture = texture;

    }

}

但是也


注意:請記住,使用 RawImage 會在每個 RawImage 出現時創建一個額外的繪制調用,因此最好僅將其用于背景或臨時可見圖形。


查看完整回答
反對 回復 2022-11-13
  • 1 回答
  • 0 關注
  • 136 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號