canvas在绘制网络地址图片时,需要先下载成临时路径
export function downLoadBgImg (url) {
return new Promise((r,j) => {
uni.downloadFile({
url,
success : res => {
if (res.statusCode === 200) {
r(res.tempFilePath);
return;
};
j('依赖文件下载失败');
},
fail : err => {
j(err)
}
})
})
}
需要用到的地方
import { downLoadBgImg } from '@/js/share.js';
shareInit(){
const ctx = uni.createCanvasContext('share-canvas', this);
return new Promise(async(r,j)=>{
try{
let bgImgUrl = await downLoadBgImg(...);
ctx.drawImage(bgImgUrl,0,0,uni.upx2px(600),uni.upx2px(480))
}catch(e){
j(e);
}
})
}