egret接4399排行榜头像跨域问题

版权声明:文章均是博主原创,如需转载,请在文章开头注明出处和署名! https://blog.csdn.net/shirln/article/details/84938909

推荐阅读:

接4399排行榜的时候,获取到头像的url地址,显示头像使用下面两种方法(注:head为url地址):
方法一:

		 this.head.source=head;

方法二:

		RES.getResByUrl(url, (data, url) => {
			egret.log("data===",url);
			this.head.source = url;
		}, this, RES.ResourceItem.TYPE_IMAGE)

就代码来看,通过url地址加载头像是没有问题的,但是想象总是美好的,可事实是这样的:

Uncaught DOMException: Failed to execute 'texImage2D' on 'WebGLRenderingContext': 
The image element contains cross-origin data, and may not be loaded.

在这里插入图片描述报错了,从日志上来看,出错原因是跨域取头像。
在尝试了各种方法,查阅了各种方法之后,通过大师的指点,使用下面方法,成功解决了这个问题,但该方法并不适用各个平台,例如QQ玩一玩暂无效。

		var imgLoader=new egret.ImageLoader;
		imgLoader.crossOrigin="anonymouse";
		imgLoader.load(head);
		imgLoader.once(egret.Event.COMPLETE,function(evt:egret.Event){
			if(evt.currentTarget.data){
				let texture=new egret.Texture();
				texture.bitmapData=evt.currentTarget.data;
				let Bitmap=new egret.Bitmap(texture);
				this.head.texture=Bitmap.texture;
			}
		},this)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/shirln/article/details/84938909
今日推荐