做微信小程序裁剪功能的時候,發(fā)現(xiàn)使用wx.canvasToTempFilePath的時候,一直裁剪出的是一張空白圖片。找了好久的原因,我以為是ctx.draw的沒成功就做了裁剪的原因,然后我這樣做了
ctx.draw(false,wepy.canvasToTempFilePath({
canvasId: 'myCanvas',
x: canvasL,
y: canvasT,
width: canvasW,
height: canvasH,
destWidth: canvasW,
destHeight: canvasH,
fileType: this.imgtype,
success: (res) => {
wepy.hideLoading()
// 成功獲得地址的地方
console.log(res.tempFilePath)
wepy.setStorage({
key: 'tempFilePaths',
data: [res.tempFilePath],
success: () => {
wepy.navigateTo({
url: 'picture'
})
}
})
// wepy.previewImage({
// current: '', // 當前顯示圖片的http鏈接
// urls: [res.tempFilePath] // 需要預覽的圖片http鏈接列表
// })
}
}))
在ctx.draw的回調(diào)函數(shù)里面執(zhí)行wx.canvasToTempFilePath,稍微有了點希望,第一次是空白圖片第二次才正常,后來發(fā)現(xiàn)可以用定時器解決這個問題!??!
ctx.drawImage(this.imageSrc)
ctx.draw(false)
setTimeout(() => {
wepy.canvasToTempFilePath({
canvasId: 'myCanvas',
x: canvasL,
y: canvasT,
width: canvasW,
height: canvasH,
destWidth: canvasW,
destHeight: canvasH,
fileType: this.imgtype,
success: (res) => {
wepy.hideLoading()
// 成功獲得地址的地方
console.log(res.tempFilePath)
wepy.setStorage({
key: 'tempFilePaths',
data: [res.tempFilePath],
success: () => {
wepy.navigateTo({
url: 'picture'
})
}
})
// wepy.previewImage({
// current: '', // 當前顯示圖片的http鏈接
// urls: [res.tempFilePath] // 需要預覽的圖片http鏈接列表
// })
}
})
}, 500)
成功解決?。?!