现在手机拍照的像素都很高,导致图片很大,再上传多张图片时很容易失败,于是研究下压缩图片,最终解决办法如下
// 先拿到临时路径 chooseSfzImg(e) { let that = this; console.log(e); uni.chooseImage({ count: 1, //默认9 sizeType: ['compressed'], success: function(res) { // 压缩 that.getImageInfo(res.tempFilePaths[0]).then(res => { that.sfzImg = res; }); } }); },
// 压缩图片 getImageInfo(src) { let _this = this; return new Promise((resolve, reject) => { uni.showLoading({ title: '压缩中...', icon: 'none' }); uni.getImageInfo({ src: src, success(res) { console.log('压缩前', 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; console.log(img); ctx.drawImage(img, 0, 0, canvasWidth / 2, canvasHeight / 2); canvas.toBlob(function(fileSrc) { let imgSrc = window.URL.createObjectURL(fileSrc); console.log('压缩后', imgSrc); resolve(imgSrc); uni.hideLoading(); }); } }); }); },
一片空白 5.8万
父爱如山,不善表达。回想十多年前,总记得父亲有个宽厚的肩膀,小小的自己跨坐在上面,越过人山人海去看更广阔的天空,那个时候期望自己有一双翅膀,能够像鸟儿一样飞得高,看得远。虽然父亲有时会和自己开玩笑,但在做错事的时候会受到严厉的训斥。父亲有双粗糙的大手掌。