網(wǎng)絡(luò)加載圖片和本地保存加載圖片

    string url = "https://upload-images.jianshu.io/upload_images/19776427-4be4c0f5c6466dc8.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240";
    public RawImage img;
    string LocalUrl = "";
    WWW w1;
   // WWW w;//網(wǎng)絡(luò)請求對象

    private bool downloadOK;

    [System.Obsolete]
    void Start()
    {
        
            //開啟攜程
            StartCoroutine(GetImage(url));
       
    }

    [System.Obsolete]
    IEnumerator GetImage(string url)
    {
        w = new WWW(url);

        yield return w;//返回?cái)?shù)據(jù)(1.成功 2、失敗 3.下載完成)

        if (w.error ==null||w.progress ==1||w.isDone==true)//所有的問題都不存在
        {
            //將下載的圖片賦值
            img.texture = w.texture;
            //存儲到本地
        }
    }
    public void SaveImage()
    {
        byte[] date = (img.texture as Texture2D).EncodeToPNG();//轉(zhuǎn)換圖片為二進(jìn)制byte[](0,1)
        File.WriteAllBytes(Application.streamingAssetsPath + "/1.png", date);//存儲圖片的二進(jìn)制到本地文件夾
        LocalUrl = "file://"+Application.streamingAssetsPath + "1.png";

        Debug.Log(LocalUrl);

        PlayerPrefs.SetString("local", LocalUrl);//保存資源路徑
    }

string url = "https://upload-images.jianshu.io/upload_images/19776427-4be4c0f5c6466dc8.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240";
    public RawImage img;
    string LocalUrl = "";
    WWW w1;
    WWW w;//網(wǎng)絡(luò)請求對象

    private bool downloadOK;

    [System.Obsolete]
    void Start()
    {
        LocalUrl = PlayerPrefs.GetString("Local");
        //加載本地存儲的照片
        if (LocalUrl != null)
        {
            Debug.Log("本地加載");
            StartCoroutine(GetImage(LocalUrl));//開啟本地加載(Get)
        }
        else
        {
            Debug.Log("網(wǎng)絡(luò)加載");
            //開啟攜程
            StartCoroutine(GetImage(url));
        }
        

    }

    [System.Obsolete]
    IEnumerator GetImage(string url)
    {
        w = new WWW(url);

        yield return w;//返回?cái)?shù)據(jù)(1.成功 2、失敗 3.下載完成)

        if (w.error ==null||w.progress ==1||w.isDone==true)//所有的問題都不存在
        {//將下載的圖片賦值
            img.texture = w.texture;
            //存儲到本地
        }
    }
    public void SaveImage()
    {
        byte[] date = (img.texture as Texture2D).EncodeToPNG();//轉(zhuǎn)換圖片為二進(jìn)制byte[](0,1)
        File.WriteAllBytes(Application.streamingAssetsPath + "/1.png", date);//存儲圖片的二進(jìn)制到本地文件夾
        LocalUrl = "file://"+Application.streamingAssetsPath + "1.png";

        Debug.Log(LocalUrl);

        PlayerPrefs.SetString("local", LocalUrl);//保存資源路徑
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

友情鏈接更多精彩內(nèi)容