主题
LimeShrink 图片压缩组件 
一个功能强大的图片压缩组件,支持多端使用。在非web端使用uni.compressImage实现,web端对齐uni.compressImage API达到全端兼容。支持单张和批量图片压缩,可自定义压缩质量、尺寸等参数。
文档链接 
📚 组件详细文档请访问以下站点:
安装方法 
代码演示 
基础用法 
使用导出的compressImage函数进行图片压缩。
js
import { compressImage, LimeShrinkImageOptions } from '@/uni_modules/lime-shrink';
uni.chooseImage({
  success(res) {
    compressImage({
      // #ifndef WEB
      src: res.tempFilePaths[0],
      // #endif
      // #ifdef WEB
      // 在web端最好是传入file文件,方便知道要使用jpg还是png算法
      // 如果传入的是路径,最好有后缀如.jpg|.png
      src: res.tempFiles[0],
      // #endif
      success(res2) {
        // #ifdef WEB
        const manager = uni
        // #endif
        // #ifndef WEB
        const manager = uni.getFileSystemManager()
        // #endif
        manager.getFileInfo({
          filePath: res.tempFilePaths[0],
          success(res){
            console.log('压缩前', res.size)
          }
        })
        manager.getFileInfo({
          filePath: res2.tempFilePath,
          success(res){
            console.log('压缩后', res.size)
          }
        })
      }
    } as LimeShrinkImageOptions)
  }
})自定义压缩参数 
可以设置压缩质量、压缩后的宽高等参数。
js
import { compressImage } from '@/uni_modules/lime-shrink';
compressImage({
  src: '/static/image.jpg',
  quality: 75, // 压缩质量,范围0~100
  compressedWidth: 500, // 压缩后的宽度
  // compressedHeight: 300, // 压缩后的高度,不设置则按比例缩放
  success(res) {
    console.log('压缩成功', res.tempFilePath);
  },
  fail(err) {
    console.error('压缩失败', err);
  }
})旧版组件方式(不推荐) 
以前的组件方式仍然保留,但不再推荐使用。
html
<l-shrink ref="shrinkRef"></l-shrink>js
const shrinkRef = ref(null)
// 支持数组
// compressImage(file|file[], options:{quality: number})
// quality: 0-100之间,默认80
const res = await shrinkRef.value.compressImage('/static/logo.png');
// 批量
const res = await shrinkRef.value.compressImage(['/static/logo.png', '/static/logo1.png'], {quality: 75});快速预览 
导入插件后,可以直接使用以下标签查看演示效果:
html
<!-- 代码位于 uni_modules/lime-shrink/components/lime-shrink -->
<lime-shrink />插件标签说明 
| 标签名 | 说明 | 
|---|---|
l-shrink | 组件标签 | 
lime-shrink | 演示标签 | 
Vue2使用说明 
main.js中添加以下代码:
js
// vue2项目中使用
import Vue from 'vue'
import VueCompositionAPI from '@vue/composition-api'
Vue.use(VueCompositionAPI)API文档 
compressImage 方法参数 
| 参数名 | 说明 | 类型 | 默认值 | 
|---|---|---|---|
| src | 图片路径或File对象,可以是相对路径、临时文件路径、存储文件路径 | string | File | - | 
| quality | 压缩质量,范围0~100,数值越小,质量越低,压缩率越高(仅对jpg有效) | number | 80 | 
| compressedWidth | 压缩后图片的宽度,单位为px,若不填写则默认以compressedHeight为准等比缩放 | number | - | 
| compressedHeight | 压缩后图片的高度,单位为px,若不填写则默认以compressedWidth为准等比缩放 | number | - | 
| algorithm | 指定压缩算法,可选值:'jpeg'、'png' | string | - | 
| success | 接口调用成功的回调函数 | function | - | 
| fail | 接口调用失败的回调函数 | function | - | 
| complete | 接口调用结束的回调函数(调用成功、失败都会执行) | function | - | 
success 回调参数 
| 参数名 | 说明 | 类型 | 
|---|---|---|
| tempFilePath | 压缩后图片的临时文件路径 | string | 
| errMsg | 接口调用结果信息 | string | 
fail 回调参数 
| 参数名 | 说明 | 类型 | 
|---|---|---|
| errCode | 错误码 | number | 
| errMsg | 错误信息 | string | 
错误码说明 
| 错误码 | 说明 | 
|---|---|
| 1101001 | 参数错误 | 
| 1101002 | 文件不存在 | 
| 1101003 | 文件读取失败 | 
| 1101004 | 文件类型不支持 | 
| 1101005 | 压缩失败 | 
| 1101006 | 保存失败 | 
| 1101007 | 图片加载失败 | 
| 1101008 | Canvas操作失败 | 
| 1101009 | 未知错误 | 
| 1101010 | 权限不足 | 
常见问题 
- Web端使用注意事项:在Web端最好传入File对象,方便确定使用jpg还是png算法。如果传入的是路径,最好有后缀如.jpg或.png。
 - 压缩质量:quality参数仅对jpg格式有效,png格式不受此参数影响。
 - 等比缩放:如果只设置了compressedWidth或compressedHeight中的一个,另一个会按照原图比例自动计算。
 
支持与赞赏 
如果你觉得本插件解决了你的问题,可以考虑支持作者:
| 支付宝赞助 | 微信赞助 | 
|---|---|
![]()  | ![]()  | 

