diff --git a/lib/pen.js b/lib/pen.js index 7a5585c..09e4569 100644 --- a/lib/pen.js +++ b/lib/pen.js @@ -184,35 +184,27 @@ export default class Painter { case 'image': { // image的长宽设置成auto的逻辑处理 const ratio = getApp().systemInfo.pixelRatio ? getApp().systemInfo.pixelRatio : 2; - if (!view.css) { + // 有css却未设置width或height,则默认为auto + if (view.css) { + if (!view.css.width) { + view.css.width = 'auto'; + } + if (!view.css.height) { + view.css.height = 'auto'; + } + } + if (!view.css || (view.css.width === 'auto' && view.css.height === 'auto')) { width = Math.round(view.sWidth / ratio); height = Math.round(view.sHeight / ratio); + } else if (view.css.width === 'auto') { + height = view.css.height.toPx(); + width = view.sWidth / view.sHeight * height; + } else if (view.css.height === 'auto') { + width = view.css.width.toPx(); + height = view.sHeight / view.sWidth * width; } else { - if (!view.css.width) { - width = Math.round(view.sWidth / ratio); - if ((!view.css.height) || view.css.height === 'auto') { - height = Math.round(view.sHeight / ratio); - } else { - height = view.css.height.toPx(); - } - } else if (view.css.width === 'auto') { - if ((!view.css.height) || view.css.height === 'auto') { - width = Math.round(view.sWidth / ratio); - height = Math.round(view.sHeight / ratio); - } else { - height = view.css.height.toPx(); - width = view.sWidth / view.sHeight * height; - } - } else { - width = view.css.width.toPx(); - if (!view.css.height) { - height = Math.round(view.sHeight / ratio); - } else if (view.css.height === 'auto') { - height = view.sHeight / view.sWidth * width; - } else { - height = view.css.height.toPx(); - } - } + width = view.css.width.toPx(); + height = view.css.height.toPx(); } break; }