[英]Angular2 - Maximum Call Stack Exceeded error when rendering images
In my Angular2 application, I want to show an image which is in Uint8Array format.在我的 Angular2 应用程序中,我想显示一个 Uint8Array 格式的图像。 But I am getting 'Maximum Call Stack Exceeded'.
但是我收到“超过最大调用堆栈”的消息。 I could render images of size ~48Kb with no error.
我可以毫无错误地渲染大约 48Kb 的图像。 But when the image size is above ~300Kb is when I am getting this error.
但是当图像大小高于 ~300Kb 时,我就会收到此错误。 This is how I am rendering the image:
这就是我渲染图像的方式:
(<HTMLInputElement>document.getElementById("imagePreview")).src = "data:image/" + type + ";base64," +
btoa(String.fromCharCode.apply(null, objData.Body));
Can someone please tell me whether I am doing it in the right way.有人可以告诉我我是否以正确的方式做这件事。 If not, please tell me how to do it correctly
如果没有,请告诉我如何正确操作
String.fromCharcode()
will run into a maximum call stack exceeded
with large string data. String.fromCharcode()
将遇到大字符串数据maximum call stack exceeded
的maximum call stack exceeded
。
To be able to convert said object to base64
you need to implement a loop based on the string length.为了能够将所述对象转换为
base64
您需要实现基于字符串长度的循环。 Something like this comes to mind:想到了这样的事情:
let img: HTMLInputElement = (<HTMLInputElement>document.getElementById("imagePreview"));
let bin : string = '';
for (let i = 0, l = objData.Body.length; i < l; i++) {
bin += String.fromCharCode(objData.Body[i]);
}
img.src = "data:image/" + type + ";base64," + btoa(bin);
Perhaps it is more efficient to chunk up the string in bigger pieces than 1 character, but that's up to you to find the most speedy way :)也许将字符串分成更大的部分比 1 个字符更有效,但这取决于您找到最快捷的方法:)
I had this issue with rendering base64 16K resolution image from DB, but it had nothing to do with the answer above.我在从 DB 渲染 base64 16K 分辨率图像时遇到了这个问题,但这与上面的答案无关。
That's the error I had.这就是我的错误。
As you can see it is caused by sanitization process.如您所见,这是由消毒过程引起的。 So in my case I had to trust the image to prevent sanitization check from running.
因此,在我的情况下,我必须信任该图像以防止运行消毒检查。 This string solved the issue for me, hope that helps someone.
这个字符串为我解决了这个问题,希望对某人有所帮助。
const safeUrl = domSanitizer.bypassSecurityTrustUrl(base64string)
Pass it like that就这样传
<img [src]="img">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.