@bam.tech/react-native-image-resizer

中文文档
3.0.11发布于 2024/11/25MITTypeScriptESM + CJS零依赖近 30 天下载 55.4万

Rescale local images with React Native

原始使用文档
文档目录8 节
  1. Setup
  2. React Native >= 0.61
  3. React Native <= 0.60
  4. Android
  5. Usage example
  6. Sample app
  7. API
  8. 👉 About Bam

React Native Image Resizer

⚠ Warning Since version 3.0.0 this library has been moved from react-native-image-resizer to @bam.tech/react-native-image-resizer

Setup

React Native >= 0.61

Since the version version 3.0.0 this package support the new architecture out of the box (Turbo Module). It also has retrocompatibility with the old one.

yarn add @bam.tech/react-native-image-resizer
cd ios && pod install

React Native <= 0.60

yarn add [email protected]
cd ios && pod install

Android

Note: on latest versions of React Native, you may have an error during the Gradle build on Android (com.android.dex.DexException: Multiple dex files define Landroid/support/v7/appcompat/R$anim). Run cd android && ./gradlew clean to fix this.

Manual linking

Manual link information for Android: Link

Usage example

import ImageResizer from '@bam.tech/react-native-image-resizer';

ImageResizer.createResizedImage(
  path,
  maxWidth,
  maxHeight,
  compressFormat,
  quality,
  rotation,
  outputPath
)
  .then((response) => {
    // response.uri is the URI of the new image that can now be displayed, uploaded...
    // response.path is the path of the new image
    // response.name is the name of the new image with the extension
    // response.size is the size of the new image
  })
  .catch((err) => {
    // Oops, something went wrong. Check that the filename is correct and
    // inspect err to get more details.
  });

Sample app

A basic, sample app is available in the example folder. It uses the module to resize a photo from the Camera Roll.

API

createResizedImage(
  /**
   * uri parameter accepts`path` or `uri`.
   * This property has been tested with the output of @bam.tech/react-native-image-picker,
   * react-native-vision-camera, @react-native-camera-roll/camera-roll and http link
   **/
  uri,
  maxWidth,
  maxHeight,
  compressFormat,
  quality,
  (rotation = 0),
  outputPath,
  (keepMeta = false),
  (options = {})
); // Returns a Promise

The promise resolves with an object containing: path, uri, name, size (bytes), width (pixels), and height of the new file. The URI can be used directly as the source of an <Image> component.

OptionDescription
pathPath of image file, or a base64 encoded image string prefixed with 'data:image/imagetype' where imagetype is jpeg or png.
widthWidth to resize to (see mode for more details)
heightHeight to resize to (see mode for more details)
compressFormatCan be either JPEG, PNG or WEBP (android only).
qualityA number between 0 and 100. Used for the JPEG compression.
rotationRotation to apply to the image, in degrees, for android. On iOS, rotation is limited (and rounded) to multiples of 90 degrees.
outputPathThe resized image path. If null, resized image will be stored in cache folder. To set outputPath make sure to add option for rotation too (if no rotation is needed, just set it to 0).
keepMetaIf true, will attempt to preserve all file metadata/exif info, except the orientation value since the resizing also does rotation correction to the original image. Defaults to false, which means all metadata is lost. Note: This can only be true for JPEG images which are loaded from the file system (not Web).
options.modeSimilar to react-native Image's resizeMode: either contain (the default), cover, or stretch. contain will fit the image within width and height, preserving its ratio. cover preserves the aspect ratio, and makes sure the image is at least width wide or height tall. stretch will resize the image to exactly width and height.
options.onlyScaleDownIf true, will never enlarge the image, and will only make it smaller.

Limitations

  • If you are using @react-native-camera-roll/camera-roll with new architecture enabled this library is not going to work. If you try to display an image with the uri of the library using <Image /> you are going to have the following error: No suitable image URL loader found for ph://.... This error come from the ReactNative ImageLoader, which is the one we are currently using. Help/PR for solving this are welcome. Until then, we recommend using react-native-image-picker.
  • Image EXIF orientation are correctly handled on Android only, But not yet on IOS #402.

👉 About Bam

We are a 100 people company developing and designing multiplatform applications with React Native using the Lean & Agile methodology. To get more information on the solutions that would suit your needs, feel free to get in touch by email or through or contact form!

We will always answer you with pleasure 😁

版本与发布记录

@bam.tech/react-native-image-resizer 共发布 15 个版本,首次发布于 2022/08/25;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.11latest2024/11/251.9 年前
3.0.102024/05/302 年前
3.0.92024/04/292 年前
3.0.82024/04/242 年前
3.0.72023/08/223 年前
3.0.62023/08/223 年前

其他发布通道:beta → 3.0.0-beta.2。

关于 @bam.tech/react-native-image-resizer 的常见问题

@bam.tech/react-native-image-resizer 是什么?

Rescale local images with React Native

如何安装 @bam.tech/react-native-image-resizer?

在项目目录运行 npm install @bam.tech/react-native-image-resizer,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(*)、react-native(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bam.tech/react-native-image-resizer 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bam.tech/react-native-image-resizer 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@bam.tech/react-native-image-resizer 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 16.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@bam.tech/react-native-image-resizer 可以商用吗?

@bam.tech/react-native-image-resizer 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bam.tech/react-native-image-resizer 的下载量有多少?

根据 npm 官方下载统计,@bam.tech/react-native-image-resizer 在 2026/10/08 – 2026/11/06 这 30 天内被下载 553,541 次,日均约 18,451 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bam.tech/react-native-image-resizer 还在维护吗?

最新版本 3.0.11 发布于 2024/11/25(1.9 年前);至今共发布 15 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/08/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。