unichooseImage count 1 sizeType compressed success res = const tempFilePaths = restempFilePaths; thisimageUrl = tempFilePaths0; thispreviewImage = temp
To compress the selected image to 1MB, you can use the uni.compressImage method after selecting the image using uni.chooseImage. Here's an example of how you can achieve this:
uni.chooseImage({
count: 1,
sizeType: ['compressed'],
success: (res) => {
const tempFilePaths = res.tempFilePaths;
const tempFilePath = tempFilePaths[0];
// Compress the image
uni.compressImage({
src: tempFilePath,
quality: 80,
success: (res) => {
const compressedFilePath = res.tempFilePath;
this.imageUrl = compressedFilePath;
this.previewImage = compressedFilePath; // Replace the preview image path
// Upload the compressed image to the backend
this.uploadImage(compressedFilePath);
},
fail: (err) => {
console.log(err);
}
});
},
});
In the example above, uni.compressImage is used to compress the image selected using uni.chooseImage. The quality option is set to 80, but you can adjust it based on your needs to achieve the desired compression level. The resulting compressed image file path is then used for further processing, such as setting it as the imageUrl and previewImage paths, and uploading it to the backend using the uploadImage function
原文地址: https://www.cveoy.top/t/topic/hSK8 著作权归作者所有。请勿转载和采集!