recursionCompressH5(url, callback, item) {
let indexObj = this;
uni.getImageInfo({
src: url,
success(res) {
let canvasWidth = res.width; //图片原始长宽
let canvasHeight = res.height;
let img = new Image();
img.src = res.path;
let canvas = document.createElement("canvas");
let ctx = canvas.getContext("2d");
canvas.width = canvasWidth / 2;
canvas.height = canvasHeight / 2;
ctx.drawImage(img, 0, 0, canvasWidth / 2, canvasHeight / 2);
canvas.toBlob(function (fileSrc) {
let imgSrc = window.URL.createObjectURL(fileSrc);
uni.getFileInfo({
filePath: imgSrc,
success:(resFileInfo) => {
if (resFileInfo.size > 1024 * 1024) {
//压缩后大于1M就继续压缩
indexObj.recursionCompressH5(imgSrc, callback, item);
return;
} else {
callback && callback(imgSrc, item)
}
},
});
});
}
});
}
uniapp H5图片上传压缩
于 2022-06-05 13:38:24 首次发布