引擎版本2.4.7
遇到一个奇怪的问,我使用XMLHttpRequest下载图片
let url = "https://img0.baidu.com/it/u=2461447895,2337530915&fm=253&fmt=auto&app=138&f=JPEG?w=387&h=388";
let xhr = new XMLHttpRequest();
if (xhr.responseType == "arraybuffer") {
let blob = new Blob([xhr.response],{ type: '' });
let objData = URL.createObjectURL(blob);
let img = new Image();
img.src = objData;
img.onload = () => {
// 创建一个 cc.Texture2D 对象
let texture = new cc.Texture2D();
texture.initWithElement(img);
texture.handleLoadedTexture();
let spriteFrame = new cc.SpriteFrame(texture);
imgNode.getComponent(cc.Sprite).spriteFrame = spriteFrame;
URL.revokeObjectURL(data);
Log.d("img load success");
};
img.onerror = (e)=>{
URL.revokeObjectURL(data);
Log.d("img load error:",JSON.stringify(e));
}
}
xhr.open(Http.Type.GET, url, true);
上面的代码在web和模拟器中没有问题,可以正常下载到图片并且初始化imgNode,在一些安卓手机上也能正常显示,但是在一些安卓设备上会出现异常,目前我在夜深模拟器出现了问题。调试看安卓日志,我JSON.stringify(spriteFrame);数据,夜深模拟器上打印的数据,_texture._src字段没有数据,看正常设备的数据是图片的base64数据,而且整个数据都比正常的少,不知道为什么会出现这个问题。有没有其他小伙伴遇到类似的问题。
我在自己的华为手机上也出现问题,多下载几次,有时候不显示,有时候会显示,显示的数据也是异常的。

