把图片的尺寸改为%或vw,不要用rpx试试
小程序h5图片压缩问题(紧急求取,老板只给24小时,夺命call)请教各位大神,本人有个项目在小程序中运行h5页面,需要使用h5进行图片压缩,目前大量用户遇到了安卓上压缩后的图片是黑图(设置底色为白色),ios上传的是白图,部分oppo手机的图片为上半张图正常,下半张图类似绿色蒙层,源码如下: let canvas = document.createElement("canvas"); let ctx = canvas.getContext('2d'); let initSize = img.src.length; let width = img.width; let height = img.height; //如果图片大于四百万像素,计算压缩比并将大小压至400万以下 let ratio; if ((ratio = width * height / 4000000) > 1) { ratio = Math.sqrt(ratio); width /= ratio; height /= ratio; } else { ratio = 1; } canvas.width = width; canvas.height = height; // 铺底色 ctx.fillStyle = "#fff"; ctx.fillRect(0, 0, canvas.width, canvas.height); //如果图片像素大于100万则使用瓦片绘制 let count; if ((count = width * height / 1000000) > 1 && Util.isIOS()) { count = Math.ceil(Math.sqrt(count)); //计算要分成多少块瓦片 // 计算每块瓦片的宽和高 var nw = ~~(width / count); var nh = ~~(height / count); // 瓦片canvas for (var i = 0; i < count; i++) { for (var j = 0; j < count; j++) { let tCanvas = document.createElement("canvas"); let tCtx = tCanvas.getContext("2d"); tCanvas.width = nw; tCanvas.height = nh; tCtx.drawImage(img, Math.ceil(i * nw * ratio), Math.ceil(j * nh * ratio), Math.ceil(nw * ratio), Math.ceil(nh * ratio), 0, 0, nw, nh); ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh); } } } else { ctx.drawImage(img, 0, 0, width, height); } let data = canvas.toDataURL('image/jpeg', 0.7); 跪谢各位大神,急急急!!!
2018-10-24