- 当前 Bug 的表现(可附上截图)
- 预期表现
- 复现路径
- 提供一个最简复现 Demo
js代码
wx.chooseImage({
count: 1, // 默认9
sizeType: [‘original’, ‘compressed’], // 可以指定是原图还是压缩图,默认二者都有
sourceType: itemIndex == 0 ? [‘album’] : [‘camera’],
success: (res) => {
console.log(res.tempFilePaths)
const item = new IMOperator(this).createNormalChatItem({
type: ‘image’,
content: res.tempFilePaths,
isMy: true,
});
that.data.chatItems.push(item);
that.setData({
chatItems: that.data.chatItems,
scrollTopVal: that.data.scrollTopVal + 999,
});
// 图片加水印功能
// var arr = that.data.fileList
// res.tempFilePaths.forEach(function(item) {
// arr.push(item)
// })
// that.setData({
// fileList: arr
// })
//获取图片详细信息
wx.getImageInfo({
src: res.tempFilePaths[0],
success: (ress) => {
console.log(ress)
let date = new Date();
let time = that.formartDate(date);
let ctx = wx.createCanvasContext(‘firstCanvas’);
that.setData({
canvasHeight: ress.height,
canvasWidth: ress.width
})
//将图片src放到cancas内,宽高为图片大小
ctx.drawImage(res.tempFilePaths[0], 0, 0, ress.width, ress.height)
//将声明的时间放入canvas
ctx.setFontSize(20) //注意:设置文字大小必须放在填充文字之前,否则不生效
ctx.setFillStyle(‘blue’)
// ctx.setGlobalAlpha(0.5)
ctx.fillText(‘15200000001’, that.data.canvasWidth - 400, that.data.canvasHeight - 150)
ctx.fillText(time, that.data.canvasWidth - 500, that.data.canvasHeight - 100)
ctx.draw(false, function() {
wx.canvasToTempFilePath({
canvasId: ‘firstCanvas’,
success: (res) => {
that.setData({
canvesimgurl:res.tempFilePath
})
},
fail: (e) => {
console.log(e)
}
})
})
wx.uploadFile({
url: ‘、、、’,
filePath: that.data.canvesimgurl,
name: ‘file’,
formData: {},
header: {
“Content-Type”: “multipart/form-data”
},
success: function (res) {
console.log(res)
var url = res.data.url;
var data = res.data.fileId;
var imgtype = “image”;
if (res.statusCode == 200) {
that.sendmessage(data, url, imgtype)
} else {
let index = parseInt(that.data.chatItems.length - 1);
that.updateViewWhenSendFailed(index)
}
console.log(res.data)
},
fail: function (res) {
console.log(res)
},
complete: function (res) {
console.log(res)
}
})
}
})
}
});
});
我也是,经过我的测试发现,好像跟canvas这个控件的width和height有关,比如你设置了50px * 50px的canvas,无论图在里面铺多大(比如100*100),你只能截在canvas里能看见的那50*50的那部分,由于我们大多是从坐标(0,0)开始铺,所以一般只能截到左上角的一部分图
你好,请提供一下出现问题的机型和微信版本,以及能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。
draw是个异步操作,你在draw的callback里调用canvasToTempFilePath是对的。但是uploadFile这个方法调用的位置不对,应该放到 canvasToTempFilePath的success回调里