const saveAndShareImage = () => {
const query = uni.createSelectorQuery();
console.log("query",query);
query.select('.snap-shot').boundingClientRect((data) => {
console.log("data", data);
console.log("draw");
uni.canvasToTempFilePath({
x: data.left,
y: data.top,
width: data.width,
height: data.height,
destWidth: data.width * 2,
destHeight: data.height * 2,
canvasId: 'myCanvas',
success: (res) => {
console.log("res1", res);
tempFilePath.value = res.tempFilePath;
console.log("tempFilePath", tempFilePath);
uni.setStorageSync('filePath', tempFilePath);
uni.showActionSheet({
itemList: ['预览图片', '保存图片'],
success: (res) => {
console.log("res2", res);
if (res.tapIndex === 0) {
uni.previewImage({
urls: [tempFilePath.value],
});
} else if (res.tapIndex === 1) {
uni.saveImageToPhotosAlbum({
filePath: tempFilePath.value,
success: () => {
uni.showToast({
title: '保存成功',
})
},
fail: () => {
uni.showToast({
title: '保存失败',
})
},
})
}
},
fail: (fail) => {
console.log("fail", fail);
},
})
},
fail: (err) => {
console.error('图片生成失败', err)
}
})
}).exec();
}
弄一个微信官方的代码片段来看看。