网络加载图片和本地保存加载图片

    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;//网络请求对象

    private bool downloadOK;

    [System.Obsolete]
    void Start()
    {
        
            //开启携程
            StartCoroutine(GetImage(url));
       
    }

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

        yield return w;//返回数据(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();//转换图片为二进制byte[](0,1)
        File.WriteAllBytes(Application.streamingAssetsPath + "/1.png", date);//存储图片的二进制到本地文件夹
        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;//网络请求对象

    private bool downloadOK;

    [System.Obsolete]
    void Start()
    {
        LocalUrl = PlayerPrefs.GetString("Local");
        //加载本地存储的照片
        if (LocalUrl != null)
        {
            Debug.Log("本地加载");
            StartCoroutine(GetImage(LocalUrl));//开启本地加载(Get)
        }
        else
        {
            Debug.Log("网络加载");
            //开启携程
            StartCoroutine(GetImage(url));
        }
        

    }

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

        yield return w;//返回数据(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();//转换图片为二进制byte[](0,1)
        File.WriteAllBytes(Application.streamingAssetsPath + "/1.png", date);//存储图片的二进制到本地文件夹
        LocalUrl = "file://"+Application.streamingAssetsPath + "1.png";

        Debug.Log(LocalUrl);

        PlayerPrefs.SetString("local", LocalUrl);//保存资源路径
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。