WeChat applets enable image compression, while WeChat applets enable Image Compression

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.