小程序, 图片压缩上传

问题: 图片上传后保存的图片跟原图不一致,还要待研究

//选择图片

  chooseImg(e) {

    let that = this;

    let imgs = this.data.imgs;

    let imgsHidden = this.data.imgsHidden;

    const token = wx.getStorageSync("jwt");

    wx.request({

      url: app.globalData.baseUrl + '/api/oss/frontend_sts_token',

      method: 'get',

      header: {

        'content-type': 'application/json',

        'Authorization': "Bearer " + token

      },

      success: res => {

        wx.chooseImage({

          count: 24, // 默认最多一次选择9张图

          sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有

          sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有

          success: function (res1) {

            let tempFilePaths = res1.tempFilePaths;

            that.setData({

              sts_token_data: res.data.data

            })

            uploadImage({

              filePath: tempFilePaths,

              sts_token_data: that.data.sts_token_data,

              success: function (res) {

                imgs.push(app.globalData.imgUrl + res);

                imgsHidden.push(res);

                that.setData({

                  imgs,

                  imgsHidden

                });

                console.log(that.data)

                wx.hideLoading();

              },

              fail: function (res) {

                console.log("上传失败");

                wx.hideLoading();

              }

            });

            // that.getCanvasImg(0, 0, tempFilePaths);           

          },

        })

      }

    })

  },

  //压缩并获取图片,这里用了递归的方法来解决canvas的draw方法延时的问题

  getCanvasImg: function (index, failNum, tempFilePaths) {

    let that = this;

    let imgs = this.data.imgs;

    let imgsHidden = this.data.imgsHidden;

    // let imgsTemp = that.data.imgsTemp;

    if (index < tempFilePaths.length) {

      const ctx = wx.createCanvasContext('attendCanvasId');

      wx.getImageInfo({

        src: tempFilePaths[index],

        success(res) {

          let width = that.data.windowWidth / 750 * 500;

          let height = res.height * width / res.width;

          that.setData({

            canvasW: width * 2, //宽度两倍

            canvasH: height * 2 //高度两倍

          })

          ctx.drawImage(tempFilePaths[index], 0, 0, width, height);

          // ctx.drawImage(tempFilePaths, 0, 0, width, height);

          ctx.draw(true, function () {

            index = index + 1;//上传成功的数量,上传成功则加1

            wx.canvasToTempFilePath({

              canvasId: 'attendCanvasId',

              success: function success(res) {

                console.log(res)

                console.log("success")

                // imgsTemp.push(res)

                // that.setData({

                //  imgsTemp

                // });

                uploadImage({

                  filePath: [res.tempFilePath],

                  sts_token_data: that.data.sts_token_data,

                  success: function (res) {

                    imgs.push(app.globalData.imgUrl + res);

                    imgsHidden.push(res);

                    that.setData({

                      imgs,

                      imgsHidden

                    });

                    console.log(that.data)

                    wx.hideLoading();

                  },

                  fail: function (res) {

                    console.log("上传失败");

                    wx.hideLoading();

                  }

                });

                setTimeout(()=>{

                  that.getCanvasImg(index, failNum, tempFilePaths);

                },800)

              }, fail: function (e) {

                console.log("fail")

                failNum += 1;//失败数量,可以用来提示用户

                setTimeout(() => {

                  that.getCanvasImg(index, failNum, tempFilePaths);

                }, 1000)

              }

            });

          });

        }

      })

    }

  },


参考: https://www.jb51.net/article/143226.htm

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 宏村的美早在多年前就已耳闻。好友家里的保姆是安徽黟县人,好友还专程在那里住过几日,流连忘返,大赞宏村如诗如...
    Summer817阅读 745评论 5 2
  • 今天上午,一位很看好的同事找我要办离职。其实今天之前,他已经来找过我两次,我和他的直线上级也先后尝试做了几次挽留,...
    白大炮阅读 301评论 0 0
  • 本文内容:1. 进程产生方式2. 进程间通信和同步3. 线程 1. 进程产生方式 产生一个进程有多种方式,如for...
    AmberXiao阅读 975评论 0 1