【发布时间】:2021-05-28 21:05:25
【问题描述】:
我正在从 REST 服务加载 arrayBuffer 图像,并且图像在显示时会减慢应用程序的速度,有没有更好的方法来做到这一点?
HTML:
<img (click)="previewPlaneringskarta()" class="planeringskarta" [src]="arrayBufferToBase64(planeringskarta)" onerror="this.onerror=null; this.src='/assets/img/notfound.png'"/>
打字稿:
arrayBufferToBase64(buffer) {
let binary = '';
const bytes = new Uint8Array(buffer);
const len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
const imageSrc = 'data:image/gif;base64,' + window.btoa(binary);
this.planeringskartaSrc = this.sanitization.bypassSecurityTrustUrl(imageSrc);
return this.planeringskartaSrc;
}
【问题讨论】:
-
不要将方法绑定到您的 [src] 属性。进行转换并仅绑定结果。
标签: html angular typescript rest arraybuffer