以Promise风格调用: 支持
压缩图片接口,可选压缩质量。iOS 仅支持压缩 JPG 格式图片。
属性 | 类型 | 默认值 | 必填 | 说明 |
|---|---|---|---|---|
| src | string | 否 | 图片路径,图片的路径,支持本地路径、代码包路径 | |
| quality | number | 80 | 否 | 压缩质量,范围0~100,数值越小,质量越低,压缩率越高(仅对jpg有效)。 |
| compressedWidth | number | 否 | 压缩后图片的宽度,单位为px,若不填写则默认以compressedHeight为准等比缩放。 | |
| compressedHeight | number | 否 | 压缩后图片的高度,单位为px,若不填写则默认以compressedWidth为准等比缩放。 | |
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
属性 | 类型 | 说明 |
|---|---|---|
| tempFilePath | string | 压缩后图片的临时文件路径 (本地路径) |
| originalSize | number | 压缩前图片的大小,单位字节 |
| compressedSize | number | 压缩后图片的大小,单位字节 |
| errMsg | string | compressImage:ok |
// 调用 compressImage 压缩图片
xhs.compressImage({
src: '/path/to/local/image.jpg', // 图片路径,支持本地路径或代码包路径
quality: 70, // 压缩质量,范围 0~100,数值越小,质量越低,压缩率越高
compressedWidth: 800, // 压缩后图片的宽度,单位 px
compressedHeight: 600, // 压缩后图片的高度,单位 px
success: (res) => {
console.log('图片压缩成功:', res);
console.log('压缩后图片路径:', res.tempFilePath);
console.log('压缩前图片大小:', res.originalSize, '字节');
console.log('压缩后图片大小:', res.compressedSize, '字节');
},
fail: (err) => {
console.error('图片压缩失败:', err.errMsg);
},
complete: () => {
console.log('compressImage 调用结束');
}
});