微信小程序图片的获取和压缩

时间:2021-01-29 12:22:12   收藏:0   阅读:0

微信小程序图片的获取和压缩

效果展示

技术图片

wxml

<view>
    <button bindtap="chooseImage">选择图片</button>
    <button bindtap="compressImage">压缩图片</button>
</view>

js

下面是js中data部分代码和绑定事件

data: {
    tempFile:‘‘,
    compressedImage:‘‘
  },
  //选择图片
  chooseImage:function(){
    var that=this;
    wx.chooseImage({
      count: 9,
      sizeType:["original","compressed"],
      sourceType:["album","camera"],
      success:function(res){
        console.log(res);
        that.setData({
          tempFile:res.tempFilePaths
        });
        wx.getImageInfo({
          src: "tempFile[0]",
          success:function(res){
            console.log(res);
          }
        })
        //预览图片
        wx.previewImage({
          urls: [that.data.tempFile[0]],         
        });
      }
    })
  },
  //压缩图片
  compressImage:function(){
    var that=this;
    wx.compressImage({
      src: that.data.tempFile[0],
      quality:40,
      success(res){
        console.log("压缩");
        var temp=res.tempFilePath;
        wx.saveImageToPhotosAlbum({
          filePath: temp,
          success(res){           
            console.log("保存成功");
            console.log(res);
          }
        })
      }
    })
  },
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!