简体   繁体   English

base64图像解码间歇地报告1x1尺寸

[英]base64 image decoding intermittently reporting 1x1 dimensions

Using ie9/ie10, I am trying to detect the dimensions of a base64 image. 我正在尝试使用ie9 / ie10来检测base64图像的尺寸。 My code works in the other current browsers. 我的代码可在其他当前浏览器中使用。 These are the steps I follow: 这些是我遵循的步骤:

  1. ajax down a base64 string, 阿贾克斯下来一个base64字符串,
  2. create a new Image() object, 创建一个新的Image()对象,
  3. set the base64 string as the src, 将base64字符串设置为src,
  4. use imagesloaded OR wait for the onload event 使用imagesloaded或等待onload事件

Moments later arrives a successful loaded callback, but... the image dimensions are reported as 1x1. 片刻之后到达成功的已加载回调,但是...图片尺寸报告为1x1。 Sometimes. 有时。 Other times it works just fine. 其他时候它工作正常。

So... how do I detect when the image is... decoded? 那么...如何检测图像何时被解码? Not sure what is going on here. 不知道这是怎么回事。

Use onload callback. 使用onload回调。 Your image is not downloaded yet when you first try to get its size. 首次尝试获取图像大小时,尚未下载图像。 That is why you get wrong size. 这就是为什么尺寸错误的原因。

image.onload = function () {
  console.log(this.width);
}

Try to use naturalWidth instead of width . 尝试使用naturalWidth而不是width It works for me in Internet Explorer 10. 它适用于Internet Explorer 10。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM