Page({
getMyInfo : function(e) {
wx.chooseImage({
sourceType : ["album"],
sizeType : ['original'],
success : function(res) {
for(let i = 0; i < res.tempFilePaths.length; i++) {
var path = res.tempFilePaths[i]
console.log(path)
var canvas = wx.createOffscreenCanvas({type: '2d'})
var ctx = canvas.getContext("2d")
// ---------------------------------------------------
// 方案 1:
// ctx.drawImage(path, 0, 0)
// var matrix = ctx.getImageData(0, 0, img.width, img.height);
// console.log(matrix);
// ---------------------------------------------------
// ----------------------------------------------------
// 方案 2:
var img = canvas.createImage()
img.src = path
img.onload = function () {
ctx.drawImage(img, 0, 0)
var matrix = ctx.getImageData(0, 0, img.width, img.height);
console.log(matrix);
}
// ----------------------------------------------------
}
}
})
}
})
上面代码中两个方案都是不行的。
第一个方案输出这样:
第二个方案输出这样:
请问有什么解决办法么?
const ctx = wx.createCanvasContext('myCanvas') wx.chooseImage({ success: function(res){ ctx.drawImage(res.tempFilePaths[0], 0, 0, 150, 100) ctx.draw() } })
const ctx = canvas.getContext()
wx.chooseImage({
success: function(res){
ctx.drawImage(res.tempFilePaths[0], 0, 0, 150, 100)
ctx.draw()
}
})