0
点赞
收藏
分享

微信扫一扫

uniapp H5图片上传压缩

DT_M 2022-06-07 阅读 242

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)
}
},
});
});
}
});
}


举报

相关推荐

0 条评论