问题描述
如果我通过网络传输一个简单的图像,它的速度很快。当我尝试使用 toDataURL
传输在画布中创建的同一图像时,它会慢得多。为什么?
If I transfer a simple image through the network, it's fast. When I try to transfer the same image created in a canvas using toDataURL
it's much slower. Why?
推荐答案
您一般不会提供过多的上下文:
You are not giving too much context so in general:
当您使用 toDataURL()
时,浏览器会将图片编码为带有小标题的Base-64流。与非编码大小相比,base-64将总是增加33%的大小。
When you use toDataURL()
the browser will encode the image as a Base-64 stream with a small header. The base-64 will always increase the size by 33% compared to non-encoded size.
如果你在本地传输JPEG编码文件,它的大小通常会更小比PNG版本的图像。如果您忘记为 toDataURL
指定图片类型,浏览器将始终默认为PNG。
If you are natively transferring a JPEG encoded file it will typically be smaller in size than a PNG version of the image. If you forget to specify image type for toDataURL
the browser will always default to PNG.
JPEG这种方式:
var quality = 0.7;
var dataUri = canvas.toDataURL('image/jpeg', quality);
这篇关于为什么toDataURL这么慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!