更新记录
0.0.6(2024-12-14)
下载此版本
0.0.5(2024-12-13)
下载此版本
0.0.4(2024-12-13)
下载此版本
查看更多
平台兼容性
App |
快应用 |
微信小程序 |
支付宝小程序 |
百度小程序 |
字节小程序 |
QQ小程序 |
Android:支持,iOS:支持,HarmonyNext:不确定 |
× |
√ |
√ |
√ |
√ |
√ |
钉钉小程序 |
快手小程序 |
飞书小程序 |
京东小程序 |
鸿蒙元服务 |
√ |
√ |
√ |
√ |
× |
H5-Safari |
Android Browser |
微信浏览器(Android) |
QQ浏览器(Android) |
Chrome |
IE |
Edge |
Firefox |
PC-Safari |
√ |
√ |
√ |
√ |
√ |
× |
× |
× |
× |
lime-shrink 图片压缩
- 在0.0.3版本之前基于uniapp vue3 canvas 实现的图片压缩插件
- 在0.0.4版本之后非web端使用uni.compressImage实现,web端对齐
uni.compressImage
API达到全端兼容。
安装
代码演示
在插件导出compressImage
函数,具体参数可以参考compressImage,文档。rotate
属性未实现。
import { compressImage, ShrinkImageOptions } from '@/uni_modules/lime-shrink';
uni.chooseImage({
success(res) {
compressImage({
// compressedWidth: 500,
// #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 ShrinkImageOptions)
}
})
旧方法 废除
以前uniapp的组件方式还保留。但不再推荐使用。如果你之前有使用该插件,麻烦使用上面的方法实现。
<l-shrink ref="shrinkRef"></l-shrink>
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});
查看示例
<!-- // 代码位于 uni_modules/lime-shrink/compoents/lime-shrink -->
<lime-shrink />
插件标签
- 默认 l-shrink 为 component
- 默认 lime-shrink 为 demo
关于vue2的使用方式
- 插件使用了
composition-api
, 如果你希望在vue2中使用请按官方的教程vue-composition-api配置
- 关键代码是: 在main.js中 在vue2部分加上这一段即可.
// main.js vue2
import Vue from 'vue'
import VueCompositionAPI from '@vue/composition-api'
Vue.use(VueCompositionAPI)
常见问题
png 无法压缩组件方式是使用canvas绘制,所以无法压缩png,0.0.4之后使用web uts api可以。
打赏
如果你觉得本插件,解决了你的问题,赠人玫瑰,手留余香。
