resizeImage(params: Object)

版本要求:
BizKit≥3.0.0@ray-js/ray≥0.3.23
支持平台:
iOSAndroid
import { resizeImage } from '@ray-js/ray';

// Promise 调用
try {
  const res = await resizeImage({ ...params });
  console.log(res);
} catch (err) {
  console.log(err.errorMsg);
}

描述

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

参数

属性类型必填说明
aspectFitWidthnumber是压缩参数 aspectFitWidth 自适应宽度
aspectFitHeightnumber是aspectFitHeight 自适应高度
maxFileSizenumber否maxFileSize 最大图片文件大小限制值,为空则不做限制,单位:B
pathstring是path 图片路径
complete() => void否接口调用结束的回调函数(调用成功、失败都会执行)
success(params: Object) => void否接口调用成功的回调函数
fail(params: Object) => void否接口调用失败的回调函数
success 回调参数
属性类型说明BizKit 最低版本
pathstring图片返回内容 path 图片路径v3.1.2
fail 回调参数
属性类型说明
errorMsgstring错误信息
errorCodestring | number错误码
错误扩展

返回值

无

示例代码

import { downloadFile, resizeImage } from '@ray-js/ray'

// Download a real image to a local path first, then resize it.
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);
    resizeImage({
      aspectFitWidth: 200,
      aspectFitHeight: 200,
      maxFileSize: 102400,
      path: path,
      success: data => {
        console.log(data);
      },
      fail: error => {
        console.error(error);
      },
    });
  },
  fail: error => {
    console.error(error);
  },
});