我们要做的就是在一个已有的图片url上加上想要的文字然后长按保存到手机
我这里使用的是 Taro 的Vue3 大家可作为一个参考转为自己的代码
第一步先定义元素
<canvas  canvas-id="myCanvas" style="width:300px; height:330px;"></canvas>
   | 
 
重新绘制图片
const drawCanvas = async() => {   const ctx = Taro.createCanvasContext('myCanvas');   const imageSrc = '你的图片url';
         const imageInfo = await Taro.downloadFile({     url: imageSrc,   }).catch(err => console.error('下载图片失败:', err))
       ctx.fillStyle = 'white';   ctx.fillRect(0, 0, 300, 330);
       ctx.drawImage(imageInfo.tempFilePath, 0, 0, 300, 300);
       ctx.setFillStyle('black')    ctx.setFontSize(24)       ctx.font = 'bold 24px sans-serif';    ctx.fillStyle = 'black';    ctx.setTextAlign('center') 
    ctx.strokeStyle = 'black';    ctx.lineWidth = 1;    ctx.strokeText('我是文字', 150, 320); 
 
       ctx.fillText(device.value.sn, 150, 320); 
       ctx.draw(true); };
  | 
 
长按保存画布
const onLongPress = (url) => {     console.log('长按');     Taro.showActionSheet({     itemList: ['保存图片'],   }).then(res => {     if (res.tapIndex === 0) {                     saveCanvasImage()     }   }).catch(err => console.error(err));   }
    const saveCanvasImage = () => {      Taro.authorize({     scope: 'scope.writePhotosAlbum',   }).then(() => {          Taro.canvasToTempFilePath({       canvasId: 'myCanvas',       success: function(res) {                  Taro.saveImageToPhotosAlbum({           filePath: res.tempFilePath,           success() {             Taro.showToast({ title: '保存成功', icon: 'success' });           },           fail() {             Taro.showToast({ title: '保存失败', icon: 'none' });           }         });       }     });   }).catch(() => {     Taro.showToast({ title: '需要授权保存相册', icon: 'none' });   }); };
  |