onClickConfirm() {
const {
dpr,
imageSrc,
x,
y,
initialScale,
scale,
clipWidth,
clipHeight,
} = this.data
const totalScale = scale * initialScale
const sx = -x / totalScale
const sy = -y / totalScale
const canvasWidth = clipWidth / totalScale
const canvasHeight = clipHeight / totalScale
this.setData({ canvasWidth, canvasHeight })
const query = wx.createSelectorQuery()
query
.select('.canvas')
.fields({ node: true, size: true })
.exec((res) => {
const canvas = res[0].node
const context = canvas.getContext('2d')
canvas.width = res[0].width * dpr
canvas.height = res[0].height * dpr
context.scale(dpr, dpr)
const canvasImg = canvas.createImage()
canvasImg.src = imageSrc
canvasImg.onload = () => {
context.drawImage(
canvasImg,
sx,
sy,
canvasWidth,
canvasHeight,
0,
0,
canvasWidth,
canvasHeight
)
wx.canvasToTempFilePath({
fileType: 'jpg',
canvas,
success: (res) => {
// TODO: 这里在实际项目中应该是调用 wx.uploadFile 来上传采集后图片
const app = getApp()
app.globalData.avatar = res.tempFilePath
var pages = getCurrentPages(); // 获取页面栈
var currPage = pages[pages.length - 1]; // 当前页面
var prevPage = pages[pages.length - 2]; // 上一个页面
var index3=this.data.index3;
prevPage.data.fielddata[index3].img_arr=prevPage.data.fielddata[index3].img_arr.concat(res.tempFilePath)
prevPage.setData({
fielddata:prevPage.data.fielddata// 假数据
})
wx.navigateBack()
},
})}
// })
})
},
你好,麻烦提供出现问题的具体机型、微信版本号、系统版本号,以及能复现问题的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)
为的是实现简单的打开图片,然后裁剪,返回