加载本地图片到工程

在游戏中微信登录一般要使用微信用户头像

lua代码

--设置玩家头像
function PlayerModel.SetUserInfo()
//this.Head为头像url,function 是去远程下载回调,远程下载内容是下载写入到本地,返回本地图片路径
	Util.DownloadImage(this.Head,function (filepash,isdone)
//开启从本地下载图片协成
			coroutine.start(this.loadImage,filepash)
	end);
end
function PlayerModel.loadImage(filepash)
//读取的时候一定要加file://前缀,jar:前缀不用加
	local www = WWW("file://"..filepash)
    Yield(www)
    if www.error ~= nil then
    	print("lua<<设置头像--加载本地图片失败");
    	print(www.error);
    else
       	MainPagePanel.HeadIcon:GetComponent("Image").sprite = Util.CreateSprite(www.texture);
    end
    www:Dispose();
end
public static void DownloadImage(int uid,string sourceUrl, LuaFunction callback)
        {
            if (!ValidateURI(sourceUrl))
            {
                callback.Call(GetImage(), false);
                return;
            }
            string localFileName = sourceUrl.Substring(sourceUrl.LastIndexOf("/") + 1);
            if (!localFileName.Contains(".png"))
            {
                localFileName = uid + localFileName + ".png";
            }
            string localPath =  DataPath + "usericon/" + localFileName;
            if (IsFileExist(localPath))
            {
                callback.Call(GetImage(localPath), true);
                return;
            }
            WWWRequest request = new WWWRequest(sourceUrl);
            WWWHelper.Instance.AddWWWTask(request, new WWWHelper.WWWBeginDownLoad(delegate ()
            {

            }),
                new WWWHelper.WWWFinishDownLoad(delegate (WWW www)
                {
                    CreatFile(www.bytes, localPath);
                    //						CreatFile(www.bytes,localFileName);
                    callback.Call(GetImage(localPath), true);
                }),
                new WWWHelper.WWWDownLoadError(delegate (WWW www, WWWRequest requestBase)
                {
                    Debug.Log("WWWDownLoadError");
                }));

        }
        /// <summary>
		/// 
		/// </summary>
		/// <param name="uri"></param>
		/// <returns></returns>
		public static bool ValidateURI(string uri)
        {
            Regex validipregex = new Regex(@"((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?");
            return (uri != "" && validipregex.IsMatch(uri.Trim())) ? true : false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns>The image.</returns>
        /// <param name="path">Path</param>
        public static string GetImage(string path = null)
        {
            if (path == null)
            {
                path = "file://" + Application.dataPath + "/Resources/Image/defaultIcon.png";
            }
            return path;
        }

        /// <summary>
        /// 判断文件是否存在
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool IsFileExist(string fullPath)
        {
            FileInfo fileInfo = new FileInfo(fullPath);
            return fileInfo.Exists;
        }

        /// <summary>
		/// 
		/// </summary>
		/// <param name="bytes"></param>
		/// <param name="file"></param>
		public static void CreatFile(byte[] bytes, string savePath)
        {
            FileInfo file = new FileInfo(savePath);
            Stream stream;
            if (!file.Directory.Exists)
            {
                file.Directory.Create();
            }
            stream = file.Create();

            stream.Write(bytes, 0, bytes.Length);
            stream.Close();
            stream.Dispose();
        }

        public static Sprite CreateSprite(Texture2D texture)
        {
            return Sprite.Create(texture, new Rect(0,0,texture.width,texture.height),Vector2.zero);
        }
    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值