Commit 998cb72c by yuzhenWang

修改海报

parent ffb43c6d
......@@ -211,10 +211,15 @@ export default {
// 调用工具函数生成图片
const imageData = await elementToImage(element);
this.generatedImage = imageData;
// 将生成的图片保存到本地存储
uni.setStorageSync('savedShareImage', imageData);
// 压缩图片
const compressedImage = await this.compressImage(imageData);
this.generatedImage = compressedImage;
uni.setStorageSync('savedShareImage', compressedImage);
// this.generatedImage = imageData;
// // 将生成的图片保存到本地存储
// uni.setStorageSync('savedShareImage', imageData);
} catch (error) {
console.error('截图失败:', error);
throw error; // 抛出错误以便外部捕获
......@@ -222,7 +227,39 @@ export default {
uni.hideLoading();
}
},
compressImage(base64) {
return new Promise((resolve) => {
const img = new Image();
img.src = base64;
img.onload = () => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
// 设置压缩后的宽高
const maxWidth = 800;
const maxHeight = 1200;
let width = img.width;
let height = img.height;
if (width > maxWidth) {
height *= maxWidth / width;
width = maxWidth;
}
if (height > maxHeight) {
width *= maxHeight / height;
height = maxHeight;
}
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0, width, height);
// 降低质量
resolve(canvas.toDataURL('image/jpeg', 0.7));
};
});
},
saveImage(base64Data) {
// 实现保存图片到相册的逻辑
// 注意:在H5中可能需要不同的处理方式
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment