get-image-info.js 742 字节
Newer Older
1 2 3 4 5 6
import getRealPath from 'uni-platform/helpers/get-real-path'

function _getServiceAddress () {
  return window.location.protocol + '//' + window.location.host
}

fxy060608's avatar
fxy060608 已提交
7 8 9 10 11 12 13
export function getImageInfo ({
  src
}, callbackId) {
  const {
    invokeCallbackHandler: invoke
  } = UniServiceJSBridge
  const img = new Image()
14
  const realPath = getRealPath(src)
fxy060608's avatar
fxy060608 已提交
15 16 17 18
  img.onload = function () {
    invoke(callbackId, {
      errMsg: 'getImageInfo:ok',
      width: img.naturalWidth,
19 20
      height: img.naturalHeight,
      path: realPath.indexOf('/') === 0 ? _getServiceAddress() + realPath : realPath
fxy060608's avatar
fxy060608 已提交
21 22 23 24 25 26 27 28
    })
  }
  img.onerror = function (e) {
    invoke(callbackId, {
      errMsg: 'getImageInfo:fail'
    })
  }
  img.src = src
X
xiaoyucoding 已提交
29
}