更新记录
1.1.2(2026-08-13)
-修复ios最新环境内联问题
1.1.1(2026-07-26)
-Android 鸿蒙 IOS 添加仿微信策略视频压缩功能 -Android 鸿蒙 IOS优化图片压缩功能参数
1.0.0(2026-07-25)
- 首发:Android / iOS / HarmonyOS 三端统一 API
- 接近微信朋友圈的自适应图片压缩
- 批量压缩、进度回调、任务取消
- 可选参数:ignoreBy / quality / focusAlpha / excludeGif / maxConcurrency / targetDir / renamePrefix
- 多语言:zh-Hans / en
- Demo:压缩前后分辨率、体积、压缩率比对
平台兼容性
uni-app(4.18)
| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
|---|---|---|---|---|---|---|---|---|
| √ | √ | × | × | √ | √ | 7.0 | √ | 4.18 |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 小红书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| × | × | × | × | × | × | × | × | - | - | - | - |
uni-app x(4.18)
| Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
|---|---|---|---|---|---|
| - | - | - | - | - | - |
其他
| 多语言 | 暗黑模式 | 宽屏模式 |
|---|---|---|
| √ | √ | √ |
ly028-Compress
📱 下载体验 APK
三端(Android / iOS / HarmonyOS)UTS 图片 + 视频 压缩插件。图片接近微信朋友圈策略;视频统一码率/分辨率算法,异步压缩、进度回调、可取消。
安装
将本插件放入项目的 uni_modules/ly028-Compress 目录(插件市场导入或手动拷贝均可)。
import {
compressImagesAsync,
compressVideoAsync,
getVideoInfoAsync,
getVideoThumbnailAsync,
clearVideoCacheAsync,
cancelCompress,
setLocale,
getLocale
} from '@/uni_modules/ly028-Compress'
快速开始
uni.chooseImage({
count: 9,
sizeType: ['original'],
success: (res) => {
const jobId = compressImagesAsync(
{ paths: res.tempFilePaths },
(progressJson) => {
const p = JSON.parse(progressJson)
// p.percent / p.current / p.total / p.phase / p.path
console.log(p.percent)
},
(resultJson) => {
const res = JSON.parse(resultJson)
if (res.code !== 0) {
uni.showToast({ title: res.msg || 'compress failed', icon: 'none' })
return
}
const data = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
// data.files: 每张图的压缩结果
console.log(data.files)
}
)
// 需要中途停止时:
// cancelCompress(jobId)
}
})
API
| 方法 | 说明 | 返回值 |
|---|---|---|
compressImagesAsync(options, , onComplete) |
批量异步图片压缩 | jobId |
compressVideoAsync(options, , onComplete) |
异步视频压缩(单任务) | jobId |
getVideoInfoAsync(path, onComplete) |
读取视频元数据 | void |
getVideoThumbnailAsync(options, onComplete) |
导出 JPEG 缩略图路径 | void |
clearVideoCacheAsync(onComplete) |
清理插件视频缓存/临时文件 | void |
cancelCompress(jobId) |
取消图片或视频任务 | boolean |
setLocale(locale) |
zh-Hans / en |
void |
getLocale() |
当前语言 | string |
压缩在后台线程执行,不会阻塞 UI。onProgress / onComplete 回到主线程,可直接更新界面。
compressImagesAsync
compressImagesAsync(
options: CompressImagesOptions,
: ((json: string) => void) | null,
onComplete: ((result: string) => void) | null
): number
options
| 字段 | 类型 | 默认 | 说明 |
|---|---|---|---|
paths |
string[] |
必填 | 图片路径列表;单张也请传数组 |
targetDir |
string |
_doc/ly028-compress |
输出目录;不传则写入应用文档目录下插件子目录(可直接用于预览) |
ignoreBy |
number |
100 |
小于该体积(KB)的图片不压缩,直接保留/拷贝 |
quality |
number |
自适应 | 可选,范围建议 5–95。不传则使用插件自适应策略(普通图固定质量,长图按目标体积自动搜索);传入则强制该 JPEG 质量 |
focusAlpha |
boolean |
false |
true:PNG / WebP 尽量保留透明通道;false:统一输出 JPEG |
excludeGif |
boolean |
true |
true:跳过 GIF;false:按静态图尝试处理 |
maxConcurrency |
number |
3 |
批量并发数,范围 1–8 |
renamePrefix |
string |
compress_ |
输出文件名前缀。最终文件名形如:{prefix}{时间戳}_{序号}.{扩展名} |
需要特殊文件名时:压缩完成后读取结果里的 outputPath,再自行拷贝/重命名即可。
onProgress(JSON 字符串)
| 字段 | 类型 | 说明 |
|---|---|---|
percent |
number |
0–100 |
current |
number |
已完成张数 |
total |
number |
总张数 |
path |
string |
当前处理的路径(可能为空) |
phase |
string |
start / compressing / item_done / done |
onComplete(JSON 字符串信封)
{
"code": 0,
"subCode": 0,
"msg": "ok",
"data": "{ ... }"
}
| 字段 | 说明 |
|---|---|
code |
0 成功,非 0 失败 |
subCode |
细分错误码(可选) |
msg |
提示文案(随 setLocale 变化) |
data |
成功时为 JSON 字符串,需再 JSON.parse |
data 解析后:
| 字段 | 类型 | 说明 |
|---|---|---|
files |
array |
每张图的结果 |
successCount |
number |
成功数 |
failCount |
number |
失败数 |
jobId |
number |
任务 ID |
files[] 单项
| 字段 | 类型 | 说明 |
|---|---|---|
sourcePath |
string |
原图路径 |
outputPath |
string |
压缩后路径(失败可能为空) |
originalSize |
number |
原图像素体积(字节) |
compressedSize |
number |
压缩后体积(字节) |
originalWidth / originalHeight |
number |
原图宽高 |
width / height |
number |
输出宽高 |
format |
string |
如 jpeg / png / webp |
costMs |
number |
单张耗时(毫秒) |
skipped |
boolean |
是否因体积过小等原因跳过压缩 |
keptOriginal |
boolean |
压缩后更大时是否回退为原图数据 |
cancelCompress
cancelCompress(jobId: number): boolean
取消后,该任务的 onComplete 仍可能回调一次(失败信封,提示已取消)。
compressVideoAsync
compressVideoAsync(
options: CompressVideoOptions,
: ((json: string) => void) | null,
onComplete: ((result: string) => void) | null
): number
options
| 字段 | 类型 | 默认 | 说明 |
|---|---|---|---|
path |
string |
必填 | 视频路径 |
preset |
string |
— | wechat / *** / social / light / maximum;先套用再被显式字段覆盖 |
quality |
number |
70 |
0–100;目标码率 = 原码率 × (0.05 + 0.95 × quality/100),再按分辨率封顶 |
maxWidth / maxHeight |
number |
不限制 | 等比缩小上限;输出宽高取偶 |
frameRate |
number |
原帧率 | 目标帧率;0/不传=保持原帧率 |
includeAudio |
boolean |
true |
false 时去掉音轨 |
audioBitrate |
number |
128000 |
音频码率(AAC) |
videoCodec |
string |
'h264' |
'h264' 或 'h265' |
ignoreBy |
number |
1024 |
忽略体积(KB)。源文件 ≤ ignoreBy×1024 字节时不压缩、直接拷贝返回;0=不按体积跳过。三端生效 |
targetDir |
string |
_doc/ly028-compress/videos |
输出目录 |
renamePrefix |
string |
video_ |
输出文件名前缀 |
快捷预设 preset
一行配置即可,对应参考库的 CompressionConfig.***() 等:
| preset | 效果 | quality | maxHeight | frameRate | audioBitrate |
|---|---|---|---|---|---|
wechat |
微信朋友圈风格 | 65 | 720 | 30 | 96000 |
*** |
*** 风格 | 65 | 720 | 30 | 96000 |
social |
社交信息流 | 70 | 1080 | 30 | 128000 |
light |
轻度压缩(保留分辨率) | 90 | 不限制 | 原帧率 | 192000 |
maximum |
极限压缩 | 30 | 480 | 24 | 64000 |
// 微信朋友圈
compressVideoAsync({ path, preset: 'wechat' }, , onComplete)
// ***-like: 720p, quality 65, 30fps
compressVideoAsync({ path, preset: '***' }, , onComplete)
// Social media: 1080p, quality 70, 30fps
compressVideoAsync({ path, preset: 'social' }, , onComplete)
// Light: keep resolution, quality 90
compressVideoAsync({ path, preset: 'light' }, , onComplete)
// Maximum: 480p, quality 30, 24fps
compressVideoAsync({ path, preset: 'maximum' }, , onComplete)
// 预设 + 覆盖个别字段
compressVideoAsync({ path, preset: 'wechat', quality: 70 }, , onComplete)
说明:
frameRate在 iOS 会写入合成帧时长;Android / Harmony 以分辨率与码率为主(系统转码 API 对帧率控制能力不一)。quality/maxHeight/audioBitrate三端均生效。
onProgress.phase
metadata / transcode / stripAudio / complete
onComplete data(解析后)
| 字段 | 说明 |
|---|---|
outputPath |
压缩后路径(_doc/...) |
originalSize / compressedSize |
字节 |
duration |
毫秒 |
originalWidth / originalHeight / width / height |
像素 |
originalBitrate / targetBitrate |
bps |
videoCodec / hasAudio / costMs / status |
编码与状态 |
getVideoInfoAsync / getVideoThumbnailAsync / clearVideoCacheAsync
getVideoInfoAsync(path, onComplete)
getVideoThumbnailAsync({ path, positionMs?, quality?, maxHeight?, targetDir? }, onComplete)
clearVideoCacheAsync(onComplete)
缩略图成功时 data 含 path(JPEG)、width、height、fileSize。
setLocale / getLocale
setLocale('zh-Hans') // 或 'en'
getLocale() // 当前语言
影响插件返回的 msg 等内置文案;不影响你业务 UI 自己的多语言。
使用注意
- 路径:相册与拍照返回的临时路径均可传入
paths。请使用uni.chooseImage/ 本地文件路径;插件会做常见路径归一化。 - 展示图片:App 端
<image>对绝对路径有时需加file://前缀,例如file:///data/.../xxx.jpg。uni.previewImage一般可直接使用返回路径。 - 输出目录:未指定
targetDir时写入_doc/ly028-compress/(应用文档目录),返回路径为_doc/...形式,可直接给<image>/uni.previewImage使用。正式业务仍可再拷贝到其它持久位置或上传后自行管理。 - 体积策略:小于
ignoreBy(默认 100KB)通常不压;压缩后若比原图更大,会回退保留原图数据(keptOriginal: true)。
平台
| 平台 | 要求 |
|---|---|
| Android | API 24+,图片高性能 JPEG;视频 Media3 Transformer |
| iOS | iOS 12+,AVFoundation |
| HarmonyOS | API 12+,AVTranscoder;去音轨使用原生 remux |
需在 App 真机或自定义基座中验证原生能力(HBuilderX 标准运行可能无法完整测试 UTS 原生插件)。
体验
📱 下载体验 APK

收藏人数:
购买源码授权版(
试用
使用 HBuilderX 导入示例项目
赞赏(0)
下载 929
赞赏 0
下载 12581293
赞赏 1949
赞赏
京公网安备:11010802035340号