[LayaAir 1.0]微信头像请求方式

手上的项目由于需要有排行榜/对战的功能, 所以需要看到对方与自己的微信头像,
网上查阅了一些代码与介绍, 但是基本都是要把微信头像缓存到自己的服务器在转发到客户端.
我尝试使用HttpRequest的方式请求数据绕过跨域, 代码如下, 经测可用
// 头像缓存
var headCache = {};
/**
* 微信头像获取器
* @param {头像Url} headUrl
* @param {显示头像Img} showImg
*/
function getWXHead(headUrl, showImg){
if(headCache[headUrl] != undefined){
showImg.skin = (headCache[headUrl]);
}else{
// 创建请求
let httpRequest = new Laya.HttpRequest();
httpRequest.once(Laya.Event.COMPLETE, this, (data)=>{
let byte = new Laya.Byte(data);
byte.writeArrayBuffer(data, 4);
let blob = new Laya.Browser.window.Blob([data], {type:'image/apng'});
let url = Laya.Browser.window.URL.createObjectURL(blob);
showImg.skin = (url);
// 缓存
headCache[headUrl] = url;
});
httpRequest.once(Laya.Event.ERROR, this, (data)=>{
// 输出错误
console.error(data);
});
// 发送请求
httpRequest.send(headUrl, '', 'get', 'arraybuffer');
}
}

QQ截图20190430171259.png

 
已邀请:

bpmf_d

赞同来自:

感谢分享!

初老青年

赞同来自:

你好 ,请问 如果原生浏览器没有Blob方法怎么办?
报错:
tmg-core.js:1 [TMA][ERROR][RUNTIME] thirdScriptErrorLaya.Browser.window.Blob is not a constructor;at api request success callback function
TypeError: Laya.Browser.window.Blob is not a constructor
 

要回复问题请先

商务合作
商务合作