片段:
https://developers.weixin.qq.com/s/8EAGqNm67Ngv
一个最简单的demo,出问题,手机上测试也没有反应,setdata给image组件没有反应
const app = getApp()
Page({
data: {
},
onLoad: function () {
wx.chooseImage({
success: (res) =>{
const tempImg = res.tempFilePaths[0];
let self = this;
//这里是重点 新版本的type 2d 获取方法
const query = wx.createSelectorQuery();
query.select('#test').fields({
node: true,
size: true
})
.exec( async (res) => {
console.log(res)
//获取canvas实例
const canvas = res[0].node;
const ctx = canvas.getContext('2d')
const dpr = wx.getSystemInfoSync().pixelRatio
canvas.width = res[0].width * dpr;
canvas.height = res[0].height * dpr;
ctx.scale(dpr, dpr);
canvas.width = 400
canvas.height = 400
//加载图片进画布
const mainImg = canvas.createImage();
mainImg.src = tempImg;
let mainImgPo = await new Promise((resolve, reject) => {
mainImg.onload = () => {
resolve(mainImg)
}
mainImg.onerror = (e) => {
reject(e)
}
});
console.log(11);
ctx.drawImage(mainImgPo, 0, 0, 333, 333, 0, 0, 320, 250);
wx.canvasToTempFilePath({
x: 100,
y: 200,
width: 50,
height: 50,
destWidth: 100,
destHeight: 100,
canvas: canvas,
success(res) {
console.log(22)
self.setData({
imgPath:res.tempFilePath
})
}
},self)
})
},
})
},
})
通过你的代码片段看到不是你setData赋值的问题
是你使用方法出现问题以下这样就可以生成图片(你之前设置那些参数导致的)
若有帮助请帮忙点个"有用"谢谢↓↓
destWidth: 100,
destHeight: 200,
canvas: canvas,
success(res) {
console.log(22)
self.setData({
imgPath:res.tempFilePath
})
}
},self)
大佬,这样写好像还是没有输出啊
若有帮助请帮忙点个"有用"谢谢↓↓
记得帮我点个有用谢谢