新的canvas的drawImage不支持本地tmp路径的临时文件吗?
发布于 7 年前 作者 yong57 9814 次浏览 来自 官方Issues

https://developers.weixin.qq.com/miniprogram/dev/api/canvas/CanvasContext.drawImage.html

drawImage( )不支持本地tmp路径的临时文件,谨慎使用,太坑了。

写法1.

```

path = ‘tmp/wxf65e9ae5f68283d2.o6zAJs5h4IkyHaGS7_j6gUPGTR9c.arwyj04Eq2ok341457e272957e237fa21d743912f60b.jpg’

ctx.drawImage(path, 0, 0, width, height, 0, 0, canvasWidth, canvasHeight)

```

提示:Failed to execute ‘drawImage’ on ‘CanvasRenderingContext2D’: The provided value is not of type ‘(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas)’;at SelectorQuery callback function

写法2.

```

const img = canvas.createImage()

img.src = path

img.onload = () =>{

    ctx.drawImage(img, 0, 0, width, height, 0, 0, canvasWidth, canvasHeight)

}

```

提示:tmp/wxf65e9ae5f68283d2.o6zAJs5h4IkyHaGS7_j6gUPGTR9c.arwyj04Eq2ok341457e272957e237fa21d743912f60b.jpg:1 GET http://tmp/wxf65e9ae5f68283d2.o6zAJs5h4IkyHaGS7_j6gUPGTR9c.arwyj04Eq2ok341457e272957e237fa21d743912f60b.jpg net::ERR_PROXY_CONNECTION_FAILED

3 回复

同问,也遇到同样问题

我也是这个需求,用的写法2,请问怎么解决????

谢邀,可以把图片放到图片服务器再配置合法域名用wx.getImageInfo或wx.downloadFile缓存到本地试试

回到顶部