uni-app uni.compressImage接口文档的参数compressHeight应改为compressedHeight才有效果

uni-app uni.compressImage接口文档的参数compressHeight应改为compressedHeight才有效果

操作步骤:

。。。

预期结果:


。。。

实际结果:


。。。

bug描述:

https://uniapp.dcloud.net.cn/api/media/image.html#compressimage 这个位置的compressHeight写错了,应该是compressedHeight才对。

2 回复

已处理 感谢反馈 ,已加分。

更多关于uni-app uni.compressImage接口文档的参数compressHeight应改为compressedHeight才有效果的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在 uni-app 的 uni.compressImage 接口中,参数 compressHeight 实际上应该为 compressedHeight 才能生效。这是一个常见的拼写错误,可能会导致开发者在使用该接口时遇到问题。

正确的参数格式

uni.compressImage({
  src: '图片路径',
  quality: 0.5, // 压缩质量,范围 0 ~ 1
  width: '100px', // 压缩后的宽度
  height: '100px', // 压缩后的高度
  compressedWidth: '100px', // 压缩后的宽度(与 width 相同)
  compressedHeight: '100px', // 压缩后的高度(与 height 相同)
  success: (res) => {
    console.log('压缩成功', res.tempFilePath);
  },
  fail: (err) => {
    console.log('压缩失败', err);
  }
});

参数说明

  • src: 要压缩的图片路径。
  • quality: 压缩质量,范围是 0 到 1,1 表示最高质量。
  • widthheight: 压缩后的图片尺寸,可以设置为百分比或像素值。
  • compressedWidthcompressedHeight: 压缩后的图片尺寸,与 widthheight 相同。

注意事项

  • compressedHeightcompressedWidth 是正确参数,而不是 compressHeightcompressWidth
  • 如果只设置 widthheight,图片会按照比例缩放。
  • 如果同时设置了 widthheight,图片会按照指定的尺寸进行压缩,可能会导致图片变形。

示例代码

uni.chooseImage({
  success: (chooseImageRes) => {
    const tempFilePaths = chooseImageRes.tempFilePaths;
    uni.compressImage({
      src: tempFilePaths[0],
      quality: 0.5,
      compressedWidth: '200px',
      compressedHeight: '200px',
      success: (compressRes) => {
        console.log('压缩成功', compressRes.tempFilePath);
      },
      fail: (err) => {
        console.log('压缩失败', err);
      }
    });
  }
});
回到顶部