收藏
回答

wx.canvasToTempFilePath保存图片格式错?

  fileType:'jpg',选择这个格式,发现保存的临时文件的后缀的确变成了jpg,但是奇怪的是文件格式还是png格式的,这个是用微信开发者工具测试的结果,请问官方如何解决

回答关注问题邀请回答
收藏

2 个回答

  • Demons
    Demons
    2022-03-10

    请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。

    2022-03-10
    有用
    回复
  • Chaowen
    Chaowen
    2022-08-08

    由于开发者工具有 BUG,只好手工粘贴代码在这里

    注:经测试,貌似些 BUG 只会在开发者工具出现,手机端好像不会出现

    js 文件

    // js
    Page({
      data: {
        canvas_info: {
          width: 2382,
          height: 2406,
          cols: 6,
          rows: 3,
          uWidth: 397,
          uHeight: 802
        },
        img: {
          url: "https://chiz.img.jwwvip.com/images/6252dbea397e68b740d1e21f.jpg?x-oss-process=image/resize,m_mfit,l_800,s_395",
          height: 800,
          width: 395,
        }
      },
      onLoad() {
        const that = this;
        wx.createSelectorQuery().select('#scheme')
          .fields({
            node: true
          })
          .exec(res => {
            const canvas = res[0].node;
            const canvas_info = that.data.canvas_info;
            const img = that.data.img;
            that.draw_image(canvas, canvas_info, img.width, img.height);
          });
      },
    
    
      draw_image(canvas, canvas_info, sWidth, sHeight) {
        const that = this;
        const ctx = canvas.getContext('2d');
        ctx.clearRect(00, canvas.width, canvas.height);
        canvas.width = canvas_info.width;
        canvas.height = canvas_info.height;
        const img_opts = that.get_img_opts(canvas_info, sWidth, sHeight);
        let success_cnt = 0;
        wx.showLoading({
          title: '正在生成中',
          mask: true,
        });
        img_opts.forEach(opt => {
          const img = canvas.createImage();
          img.onload = () => {
            ctx.drawImage(
              img, opt.sx, opt.sy, opt.sWidth, opt.sHeight,
              opt.dx, opt.dy, opt.dWidth, opt.dHeight,
            );
            success_cnt += 1;
            if (success_cnt === img_opts.length) {
              wx.canvasToTempFilePath({
                canvas: canvas,
                quality: 0.8,
                fileType: "jpg",
                success: res => {
                  console.log(res.tempFilePath);
                  that.setData({
                    path: res.tempFilePath
                  });
                  wx.hideLoading();
                }
              });
            }
          };
          img.src = opt.url;
        });
      },
    
    
      get_img_opts: function (canvas_info, sWidth, sHeight) {
        const that = this;
        const result = [];
        for (let i = 0; i < canvas_info.rows; i++) {
          for (let j = 0; j < canvas_info.cols; j++) {
            result.push({
              url: that.data.img.url,
              sx: 0,
              sy: 0,
              sWidth: sWidth,
              sHeight: sHeight,
              dx: canvas_info.uWidth * j,
              dy: canvas_info.uHeight * i,
              dWidth: sWidth,
              dHeight: sHeight,
            });
          }
        }
        return result;
      },
    
    
      preview () {
        const that = this;
        wx.previewImage({
          urls: [that.data.path],
        });
      },
    
    
      save() {
        const that = this;
        wx.saveImageToPhotosAlbum({
          filePath: that.data.path,
        });
      }
    });
    

    wxml 文件

    <view class="img_box" style="height:432rpx;">
        <canvas bindtap="preview" id="scheme" type="2d" style="width:400rpx; height:400rpx"></canvas>
    </view>
    
    
    <button type="primary" bindtap="save">保存</button>
    

    wxss 文件

    .img_box {
      width100%;
      display: flex;
      justify-content: center;
      align-items: center;
      border-bottom1px solid rgba(1281281280.2);
    }
    





    2022-08-08
    有用
    回复
登录 后发表内容