小程序
小游戏
企业微信
微信支付
扫描小程序码分享
wx.canvasGetImageData真机或者调试环境没走回调
3 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
wx.canvasGetImageData真机或者调试环境没走回调这个问题给下可复现问题的代码片段:https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
写个代码片段:https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html
代码片段呢
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
wx.canvasGetImageData真机或者调试环境没走回调这个问题给下可复现问题的代码片段:https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html
// canvasId: 'compressCanvas',
// x: 0,
// y: 0,
// width: canvasW,
// height: canvasH,
// success: function (res) {
// console.log('canvasGetImageData success')
// let pngData = upng.encode([res.data.buffer], res.width, res.height)
// let bs64 = wx.arrayBufferToBase64(pngData)
// resolve('data:image/jpeg;base64,' + bs64)//最终图片路径
// },
// fail: function (err) {
// console.log(err)
// }
// })
换成这段试试呢
写个代码片段:https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html
代码片段呢
function compressImg(filePath, that) {
return new Promise((resolve, reject) => {
wx.getImageInfo({
src: filePath,
success: res => {
let canvasW = res.width;
let canvasH = res.height;
let radio = Math.trunc(canvasW / 512) == 0 ? 1 : Math.trunc(canvasW / 512);
canvasW = Math.trunc(res.width / radio);
canvasH = Math.trunc(res.height / radio);
that.setData({
cWidth: canvasW,
cHeight: canvasH
})
var ctx = wx.createCanvasContext('compressCanvas')
ctx.drawImage(res.path, 0, 0, canvasW, canvasH)
ctx.draw(false, setTimeout(() => {
wx.canvasGetImageData({
canvasId: 'compressCanvas',
x: 0,
y: 0,
width: canvasW,
height: canvasH,
success: function (res) {
console.log('canvasGetImageData success')
let pngData = upng.encode([res.data.buffer], res.width, res.height)
let bs64 = wx.arrayBufferToBase64(pngData)
resolve('data:image/jpeg;base64,' + bs64)//最终图片路径
},
fail: function (err) {
console.log(err)
}
})
}, 500))
}
})
})
}