//将url加载为img Element
function LoadImage(src){
var img=$("<img>")
img.css("display","none")
img.attr("crossOrigin","Anonymous")
img.attr("src",src)
$("body").append(img)
return img.get(0)
}
//将arr_buffer解码成img Element
function get_image(arr_buffer){
var uInt8Array = new Uint8Array(arr_buffer)
var i = uInt8Array.length;
var binaryString = new Array(i);
while (i--)
{
binaryString[i] = String.fromCharCode(uInt8Array[i]);
}
var data = binaryString.join('');
var base64 = window.btoa(data);
if(src.endsWith("png"))
var url="data:image/png;base64," + base64;
else if(src.endsWith("gif"))
var url="data:image/gif;base64," + base64;
return LoadImage(url)
}
运行示例:
$("body").append(get_image(arr_buffer))