canvas压缩图片问题
发布于 5 年前 作者 nalong 19766 次浏览 来自 问答

我在手机上选择一张图片之后进行canvas压缩重新生成图片,然后用canvasToTempFilePath导出图片再上传到后台,可是后台处理不了这个图片,不压缩图片传到后台实在太大了。请问该如何去压缩图片。

var types = e.currentTarget.dataset.zp

   var that = this;

   wx.chooseImage({

     count: 1,

     sizeType: [‘compressed’],

     success: function(photo) {

       wx.getImageInfo({

         src: photo.tempFilePaths[0],

         success: function(res) {

           console.log(res)

           var ctx = wx.createCanvasContext(‘attendCanvasId’);          

           //设置canvas尺寸

           ctx.drawImage(photo.tempFilePaths[0], 0, 0, 300, 160)

           //将图片填充在canvas上        

           ctx.draw()

           //下载canvas图片

           setTimeout(function() {

             wx.canvasToTempFilePath({

               canvasId: ‘attendCanvasId’,

               success: function(res) {

                 console.log(res.tempFilePath)

                 if (types == ‘zm’){

                   that.setData({

                     zm: res.tempFilePath,

                   })

                 }else{

                   that.setData({

                     fm: res.tempFilePath,

                   })

                 }                      

               }

             })

           }, 100)

         }

       })

     }

   })

1 回复

canvasToTempFilePath 建议你放到 ctx.draw的回调里哈。。

生成的图,能放个链接出来吗?

回到顶部