---
name: "resizeImage"
mode: "kit"
versionRequirements:
  - { name: "BizKit", version: "3.0.0" }
platform:
  - "iOS"
  - "Android"
async: true
title: "resizeImage - 压缩图片， 在保持原图长宽比基础上先裁剪至目标尺寸， 然后根据文件大小限制去执行质量压缩"
---

## resizeImage

> [VERSION] BizKit >= 3.0.0

> [PLATFORM] iOS, Android

> ⚡ **支持 Promise 调用** — 不传 success / fail / complete 回调时，该方法返回 Promise。

### 描述

压缩图片， 在保持原图长宽比基础上先裁剪至目标尺寸， 然后根据文件大小限制去执行质量压缩

### 参数

| 属性 | 类型 | 必填 | 默认值 | 最低版本 | 描述 |
| --- | --- | --- | --- | --- | --- |
| `aspectFitWidth` | `number` | 是 | - | `3.0.0` | 压缩参数 aspectFitWidth 自适应宽度 |
| `aspectFitHeight` | `number` | 是 | - | `3.0.0` | aspectFitHeight 自适应高度 |
| `maxFileSize` | `number` | 否 | - | `3.0.0` | maxFileSize 最大图片文件大小限制值，为空则不做限制，单位:B |
| `path` | `string` | 是 | - | `3.0.0` | path 图片路径 |
| `complete` | `() => void` | 否 | - | - | 接口调用结束的回调函数（调用成功、失败都会执行） |
| `success` | `(params: Object) => void` ↓见下方 | 否 | - | - | 接口调用成功的回调函数 |
| `fail` | `(params: Object) => void` ↓见下方 | 否 | - | - | 接口调用失败的回调函数 |

#### success 回调参数

| 属性 | 类型 | 最低版本 | 描述 |
| --- | --- | --- | --- |
| `path` | `string` | `3.1.2` | 图片返回内容 path 图片路径 |

#### fail 回调参数

| 属性 | 类型 | 描述 |
| --- | --- | --- |
| `errorMsg` | `string` | 错误信息 |
| `errorCode` | `string \| number` | 错误码 |
| `innerError` | `Object` | 错误扩展 |

##### fail(params).innerError 的属性

| 属性 | 类型 | 描述 |
| --- | --- | --- |
| `errorCode` | `string \| number` | 错误扩展码 |
| `errorMsg` | `string` | 错误扩展信息 |


### 示例代码

```tsx
// Download a real image to a local path first, then resize it.
ty.downloadFile({
  url:
    "https://images.tuyacn.com/rms-static/b7bed040-87b8-11eb-864d-a9f07cbbdf37-1616051299140.png?tyName=logo_cn.png",
  success: res => {
    const path = res.tempFilePath || res.filePath || res.apFilePath;
    console.log("downloaded", path);
    ty.resizeImage({
      aspectFitWidth: 200,
      aspectFitHeight: 200,
      maxFileSize: 102400,
      path: path,
      success: data => {
        console.log(data);
      },
      fail: error => {
        console.error(error);
      },
    });
  },
  fail: error => {
    console.error(error);
  },
});
```
