从 Javascript 变量中的数据创建 HTML 图像
我在 Javascript 变量中有图像数据(JPEG 或 PNG)。如何在 HTML 文档中显示图像?这些是非常大的图像,这样的代码不起作用,因为 URI 太长:
// doesn't work because the URI is too long
$('img#target').attr('src', 'data:...');
使用画布可能是答案,但我不知道如何使用图像数据加载它。
如果您想知道:不,我不能直接将图像数据下载到 img 标签。它来自加密的服务器,并在浏览器中使用 JavaScript 进行解密。
谢谢,
——艺术Z。
I have image data (either JPEG or PNG) in a Javascript variable. How do I display the image in an HTML document? These are very large images and code like this doesn't work because the URI is too long:
// doesn't work because the URI is too long
$('img#target').attr('src', 'data:...');
Using canvas is probably the answer but I cannot figure out how to load it with the image data.
In case you are wondering: no, I cannot just download the image data directly to the img tag. It came from the server encrypted and was decrypted within the browser using Javascript.
Thanks,
-- Art Z.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要使用数据 URL 在画布上绘图:
Per 这个问题/答案请确保在
src
之前设置onload
。您说“URI 太长”,但不清楚您的意思。仅IE8对数据URI有32kB的限制;对于其他浏览器,它应该可以正常工作。如果您遇到错误,请描述它。
To use a data URL to draw on a canvas:
Per this question/answer be sure that you set the
onload
before thesrc
.You say that "the URI is too long", but it is not clear what you mean by this. Only IE8 has a 32kB limit on the data URI; for other browsers it should work fine. If you are experiencing an error, please describe it.
事实证明,
除了 IE 之外,这一切都有效。我的问题源于其他地方。
It turns out that
does work in all except IE. My problem originated elsewhere.