Compress images by using applets
In the initial stage, the little program of Brother Xiaolong is equivalent to 6 in the IE field. Here we will tell you a glimpse of the past.
Photo API.
Wx. chooseImage ({count: 1, // default 9 sizeType: ['original', 'compressed '], // you can specify whether the source image or the compressed image is used. Both have sourceType by default: ['alipay', 'camera '], // you can specify whether the source is an album or a camera. Both have success: function (res) by default) {// return the local file path list of the selected image. The tempFilePath can be used as the src attribute of the img label to display the image var tempFilePaths = res. tempFilePaths ;}});
In the above section, the type of size is clearly given, which is intended to save time, but it is useless .....
Let's talk about the difference between IOS and Android, as well as the image compression pitfalls.
// Click the camera takePictures: function () {var that = this; wx. chooseImage ({count: 1, // default 9 sizeType: ['compressed '], // you can specify the source image or a compressed image. Both have sourceType by default: ['camera '], // you can specify whether the source is an album or a camera. By default, both have success: function (res) {// return the list of local file paths of the selected photo, tempFilePath can be used as the src attribute of the img label to display the image var tempFilePaths = res. tempFilePaths; that. setData ({attendSuccessImg: tempFilePaths [0]}); // upload an image // identify the model 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 ();} // scaled image function drawCanvas () {const ctx = wx. createCanvasContext ('attendcanvasid'); ctx. drawImage (tempFilePaths [0], 0, 0, 94, 96); ctx. draw (); that. prodImageOpt () ;}}) ;}, // generate the image prodImageOpt: function () {var that = this; wx. canvasToTempFilePath ({canvasId: 'attendcanvasid', success: function success (res) {that. setData ({canvasImgUrl: res. tempFilePath}); // upload the image that. uploadFileOpt (that. data. canvasImgUrl) ;}, complete: function complete (e ){}});},
After you click to take a picture, IOS compresses the image. However, Android is still so large, so in this process, we need to determine the current model and then perform canvas compression.
The above code is ready for use, but a canvas label needs to be added to a small part of wxml.
To call the API. I hope to help you.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.