微信小程序div生成图片,保存到手机相册

//HTML
<canvas canvas-id="canvas" style="width:328px;height:232px"></canvas>
//js
 //保存图片
    //生成img
    getCanvasImg() {
      wx.showLoading({
        title: "图片生成中...", //提示的内容,
        mask: true, //显示透明蒙层,防止触摸穿透,
        success: res => {
          var path = "https://****/****.jpg";
          var that = this;
          // 服务器相片下载到本地
          wx.downloadFile({
            url: path,
            success(res) {
              console.log("downloadFile", res);
              if (res.statusCode === 200) {
                console.log(res.tempFilePath);
                // 生命周期函数--监听页面初次渲染完成
                var ctx = wx.createCanvasContext("canvas");
                ctx.drawImage(res.tempFilePath, 0, 0, 328, 232);
                ctx.setFontSize(9);
                ctx.setFillStyle("#333333");
                if (that.nickName == null) {
                  ctx.fillText(
                    that.getData && that.getData.nickName
                      ? that.getData.nickName + "  同志:"
                      : "战疫  同志:",
                    35,
                    124
                  );
                } else {
                  ctx.fillText(that.nickName + "  同志:", 35, 124);
                }
                ctx.fillText(that.bindTime, 235, 205);
                ctx.stroke();
                ctx.draw(false, res => {
                  // 生成图片保存本地
                  wx.canvasToTempFilePath({
                    canvasId: "canvas",
                    success(res) {
                      console.log("canvasToTempFilePath", res);
                      // 保存到手机相册
                      wx.saveImageToPhotosAlbum({
                        filePath: res.tempFilePath,
                        success(res) {
                          console.log("saveImageToPhotosAlbum", res);
                          wx.showToast({
                            title: "保存成功",
                            icon: "success"
                          });
                        },
                        fail(err) {
                          console.log("失败保存", err);
                          wx.showToast({
                            title: "保存失败", //提示的内容,
                            icon: "none", //图标,
                            duration: 2000 //延迟时间,
                          });
                        }
                      });
                    }
                  });
                });
                console.log(ctx);
              }
            },
            fail(err) {
              console.log("downloadFile Error:", err);
            }
          });
        }
      });
    },
    //保存图片
    saveAlbum() {
      let that = this;
      // 相册授权
      wx.getSetting({
        success(res) {
          // 进行授权检测,未授权则进行弹层授权
          if (!res.authSetting["scope.writePhotosAlbum"]) {
            wx.authorize({
              scope: "scope.writePhotosAlbum",
              success() {
                //下载到本地
                that.getCanvasImg();
              },
              // 拒绝授权
              fail() {
                that.saveAlbumImgOne = false;
                that.saveAlbumImgTow = true;
              }
            });
          } else {
            // 已授权则直接进行保存图片
            that.getCanvasImg();
          }
        },
        fail(res) {
          console.log(res);
        }
      });
    },
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值