unity3d:url下载头像保存在本地(微信头像)

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.IO;
using System.Collections.Generic;

public class AsyncImageDownload : MonoBehaviour
{
    Dictionary<int, Sprite> m_dicHeadSpr = new Dictionary<int, Sprite>();

    private static AsyncImageDownload _instance = null;
    public static AsyncImageDownload GetInstance() { return Instance; }
    public static AsyncImageDownload Instance
    {
        get
        {
            if (_instance == null)
            {
                GameObject obj = new GameObject("AsyncImageDownload");
                _instance = obj.AddComponent<AsyncImageDownload>();
                DontDestroyOnLoad(obj);
                _instance.Init();
            }
            return _instance;
        }
    }

    public bool Init()
    {
        if (!Directory.Exists(Application.persistentDataPath + "/ImageCache/"))
        {
            Directory.CreateDirectory(Application.persistentDataPath + "/ImageCache/");
        }
        
        return true;
    }

    public void StartUp()
    {

    }

    public void SetAsyncImage(string url, Image image)
    {
        //开始下载图片前,将UITexture的主图片设置为占位图
        int code = url.GetHashCode();
        if (m_dicHeadSpr.ContainsKey(code))
        {
            image.sprite = m_dicHeadSpr[code];
        }
        else       //如果之前不存在缓存中  就用WWW类下载
        {
            //判断是否是第一次加载这张图片
            if (!File.Exists(path + code.ToString() + ".txt"))
            {
                //如果之前不存在缓存文件
                StartCoroutine(DownloadImage(url, image,code));
            }
            else
            {
                StartCoroutine(LoadLocalImage(url, image,code));
            }
        }
    }

    IEnumerator DownloadImage(string url, Image image, int code)
    {
        WWW www = new WWW(url);
        yield return www;

        Texture2D tex2d = www.texture;



        Sprite sprite = Sprite.Create(tex2d, new Rect(0, 0, tex2d.width, tex2d.height), new Vector2(0, 0));
        image.sprite = sprite;

        byte[] pngData = tex2d.EncodeToPNG();
        File.WriteAllBytes(path + code.ToString() + ".txt", pngData);

        m_dicHeadSpr[code] = sprite;
    }

    IEnumerator LoadLocalImage(string url, Image image,int code)
    {
        string filePath = "file:///" + path + code.ToString() + ".txt";
        //     Debug.Log("getting local image:" + filePath);
        WWW www = new WWW(filePath);
        yield return www;

        Texture2D texture = www.texture;
        Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0, 0));
        image.sprite = sprite;
        m_dicHeadSpr[code] = sprite;

    }

    public string path
    {
        get
        {
            //pc,ios //android :jar:file//
            return Application.persistentDataPath + "/ImageCache/";

        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

四夕立羽

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值