錯誤原因:
生成海報為了方便調(diào)用,將canvas封裝在了子組件中。
繪制完海報后,調(diào)用uni.canvasToTempFilePath()
保存canvas為圖片會提示錯誤信息:canvasToTempFilePath: fail canvas is empty
通過百度了解,在微信小程序子組件中調(diào)用canvasToTempFilePath
時,需要額外增加一個this
uni.canvasToTempFilePath(object, component,this)
文章來源地址http://www.zghlxwxcb.cn/news/detail-502591.html
ctx.draw(true, (ret) => { // draw方法 把以上內(nèi)容畫到 canvas 中。
setTimeout(()=>{
uni.canvasToTempFilePath({ // 保存canvas為圖片
canvasId: 'myCanvas',
quality: 1,
width:this.canvasW,
height:this.canvasH,
destWidth:this.canvasW,
destHeight:this.canvasH,
complete: (res)=> {
// 在H5平臺下,tempFilePath 為 base64,
// 圖片提示跨域 H5保存base64失敗,APP端正常輸出臨時路徑
if(res.tempFilePath){
// 小程序中,將res.tempFilePath轉(zhuǎn)為base64,并保存圖片
uni.getFileSystemManager().readFile({
filePath: res.tempFilePath,
encoding: 'base64',
success: res => {
let base64 = 'data:image/png;base64,' + res.data;
this.$http.post("Users/base64_upload", {
image: base64
}).then(res => {
if (res.code == 200) {
uni.showToast({
icon: 'success',
mask: true,
title: '完成',
});
this.ewm = this.$imgUrl+res.data;
}
})
}
})
}
},
},this) // 這里是新增的this,只有在微信小程序中需要加入,不然會報錯
},1000)
});
文章來源:http://www.zghlxwxcb.cn/news/detail-502591.html
到了這里,關于uniapp:微信小程序uni.canvasToTempFilePath(),報錯:canvasToTempFilePath: fail canvas is empty的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!