canvas 压缩图片问题?
发布于 6 年前 作者 chengwei 12692 次浏览 来自 问答

      const ctx = wx.createCanvasContext(‘CanvasId’);

      console.log(this.data.front)//图片临时地址

      wx.getImageInfo({

            src: this.data.front,

            success: (res) => {

                  var ratio = this.data.pixelRatio

                  if (res.width > size) { // 判断图片是否超过300像素

                        // var scale = res.height / res.width

                        this.setData({

                              cWidth:Math.trunc(res.width / ratio),

                              cHeight:Math.trunc(res.height / ratio)

                        })

                        // 画出压缩图片

                        ctx.drawImage(this.data.front, 0, 0, this.data.cWidth, this.data.cHeight);

                        ctx.draw(false,                                    

                              setTimeout(() => {

                                    wx.canvasToTempFilePath({

                                          canvasId: ‘CanvasId’,

                                          fileType: ‘jpg’,

                                          quality:1,

                                          success: (res) => {

                                                console.log(res.tempFilePath)

 

                                          },

                                          fail: (res) => {

                                                console.log(res)

                                          }

                                    })

                              },300)

  

                        )

 

            }

      });

压缩的图片比原来的图片还大,不知道怎么回事

1 回复

quality用0-1 1就是全质量 你用0.5试试

回到顶部