小程序
小游戏
企业微信
微信支付
扫描小程序码分享
本人小白一名,最近在研究如何进行压缩裁剪过的图片,借鉴网上的代码时,电脑调试时完全没有问题,但是一到手机测试时,有两个大问题。一个是反应非常慢,一个500多kb的截图压缩到128kb以下,需要30多s甚至更多;第二个是压缩后的结果图片会泛白。这样的问题在电脑测试过程中从来没有过,想知道为什么会这样,以及有什么办法解决。急急急!!!跪谢各位大佬!!!
原图:
手机结果图:
2 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
我是直接把图片压缩到最长边为720 质量压缩成85 不用循环压缩 很快的
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
<canvas id="canvas" type="2d" canvas-id="canvas"></canvas>
#canvas{
visibility: hidden;
position:absolute;
z-index: -1;
left: -10000rpx;
top:-10000rpx;
width: 1000rpx;
height: 1000rpx;
}
js调用
wx.chooseImage({
count:1,
sizeType: ['compressed'],
success: function (res) {
console.log(res.tempFilePaths[0])
util.getLessLimitSizeImage('canvas', res.tempFilePaths, _this.data.maxSize)
.then(async(imgs)=>{
})
.catch((err)=>{
wx.showToast({
title: err,
icon:'none',
mask:'true'
压缩图片的思路就是通过不断的递归一直压缩到图片大小小于设定的大小,下面是压缩主函数:
function getCanvasImage(canvasId, imagePath, imageW, imageH, getImgsuccess) {
const ctx = wx.createCanvasContext(canvasId);
ctx.drawImage(imagePath, 0, 0, imageW, imageH);
ctx.draw(false, setTimeout(function() {
wx.canvasToTempFilePath({
canvasId: canvasId,
x: 0,
y: 0,
width: imageW,
height: imageH,
quality: 1,
success: function(res) {
getImgsuccess(res.tempFilePath);
},
});
}, 200));
};
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
我是直接把图片压缩到最长边为720 质量压缩成85 不用循环压缩 很快的
<canvas id="canvas" type="2d" canvas-id="canvas"></canvas>#canvas{visibility: hidden;position:absolute;z-index: -1;left: -10000rpx;top:-10000rpx;width: 1000rpx;height: 1000rpx;}js调用wx.chooseImage({count:1,sizeType: ['compressed'],success: function (res) {console.log(res.tempFilePaths[0])util.getLessLimitSizeImage('canvas', res.tempFilePaths, _this.data.maxSize).then(async(imgs)=>{}).catch((err)=>{wx.showToast({title: err,icon:'none',mask:'true'})})})压缩图片的思路就是通过不断的递归一直压缩到图片大小小于设定的大小,下面是压缩主函数:
function getCanvasImage(canvasId, imagePath, imageW, imageH, getImgsuccess) {
const ctx = wx.createCanvasContext(canvasId);
ctx.drawImage(imagePath, 0, 0, imageW, imageH);
ctx.draw(false, setTimeout(function() {
wx.canvasToTempFilePath({
canvasId: canvasId,
x: 0,
y: 0,
width: imageW,
height: imageH,
quality: 1,
success: function(res) {
getImgsuccess(res.tempFilePath);
},
});
}, 200));
};