因为项目登录需要图形验证,接口直接返回图片流,根据以往做法直接装blod类型是可以显示的
// res arraybuffer 对象集
const blob=new Blob([res],{ type: 'image/jpeg' });
// 创建一个URL对象
const imgUrl=URL.createObjectURL(blob);
但是这种做法在h5正常像是,在app端却无法显示,需要转成base64
// 将arraybuffer转换为base64字符串
export function arrayBufferToBase64(arrayBuffer) {
let base64 = "";
const bytes = new Uint8Array(arrayBuffer);
const len = bytes.byteLength;
for (let i = 0; i < len; i++) {
base64 += String.fromCharCode(bytes[i]);
}
return `data:image/png;base64,${btoa(base64)}`;
}
仅做问题记录