微信小程式實現圖片壓縮功能,微信小程式圖片壓縮

來源:互聯網
上載者:User

小程式實現圖片壓縮功能,小程式圖片壓縮

小龍大哥的小程式在初始階段相當於IE界的6,在這裡給大家說一個剛趟過去的坑。

拍照的API。

wx.chooseImage({ count: 1, // 預設9 sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,預設二者都有 sourceType: ['album', 'camera'], // 可以指定來源是相簿還是相機,預設二者都有 success: function (res) {   // 返回選定照片的本地檔案路徑列表,tempFilePath可以作為img標籤的src屬性顯示圖片  var tempFilePaths = res.tempFilePaths;  }});

在上邊,明確的給出大小的類型,本想省事,然並沒有什麼用…..
廢話少說,給大家說下IOS和安卓中差別,拍照圖片壓縮的坑。

// 點擊照相 takePictures:function(){ var that = this; wx.chooseImage({  count: 1, // 預設9  sizeType: ['compressed'], // 可以指定是原圖還是壓縮圖,預設二者都有  sourceType: ['camera'], // 可以指定來源是相簿還是相機,預設二者都有  success: function (res) {  // 返回選定照片的本地檔案路徑列表,tempFilePath可以作為img標籤的src屬性顯示圖片  var tempFilePaths = res.tempFilePaths;  that.setData({   attendSuccessImg:tempFilePaths[0]  });  // 上傳圖片  //判斷機型  var model = "";  wx.getSystemInfo({   success:function(res){   model= res.model;   }  })  if(model.indexOf("iPhone") <= 0){   that.uploadFileOpt(that.data.attendSuccessImg);   console.log(111111)  }else{   drawCanvas();  }  // 縮放圖片  function drawCanvas(){   const ctx = wx.createCanvasContext('attendCanvasId');   ctx.drawImage(tempFilePaths[0], 0, 0, 94, 96);   ctx.draw();   that.prodImageOpt();  }  } }); }, // 產生圖片 prodImageOpt:function(){ var that = this; wx.canvasToTempFilePath({   canvasId: 'attendCanvasId',  success: function success(res) {  that.setData({   canvasImgUrl:res.tempFilePath  });  // 上傳圖片  that.uploadFileOpt(that.data.canvasImgUrl);  },  complete: function complete(e) {  } }); },

再點擊拍照後,IOS的進行了圖片壓縮功能,然而,安卓的依然是那麼大,所以 在這過程中,我們需要判斷下當前機型,然後執行canvas壓縮。

上述代碼,拿到即可用,但少一部分wxml中需要添加一個canvas標籤。

進行介面調用。希望對大家有協助。

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.