React Native Android] 调整图片大小并上传到后端服务器

Max*_*s S 1 android react-native

用该设备拍摄的照片很大。我想在将它们调整大小(缩放它们)到更合理的大小(小于 800x800)后将它们上传到我的后端服务器。我希望使用ImageEditor模块的coprImage()功能,但是使用大图像运行它会导致OutOfMemory异常。我假设由于模块尝试解码大图像并将其存储在内存中,因此应用程序崩溃了。

我需要的是以下内容:

输入

{
  width: 3100,
  height: 2500,
  uri: content://android/1 (some location in Android device)
}
Run Code Online (Sandbox Code Playgroud)

输出

{
  width: 800,
  height: 650,
  uri: content://android/1/resized (some location in Android device)
}
Run Code Online (Sandbox Code Playgroud)

然后我可以抓取这个 uri 将图片发送到我的后端服务器,并从设备中删除调整大小的照片。

我假设我将不得不编写一个NativeModule这样我就可以调整图像大小而无需将大的解码图像加载到内存中。React Native 的Image组件在渲染它们之前使用Fresco来处理调整大小,但我认为它没有提供一种调整图像大小并将其临时保存在 fs 中的方法。

任何帮助,将不胜感激。

参考:

  1. https://developer.android.com/training/displaying-bitmaps/load-bitmap.html
  2. http://frescolib.org/docs/resizing-rotating.html
  3. https://facebook.github.io/react-native/docs/images.html
  4. Android中的内存高效图像调整大小

小智 7

Expo 库有一个可以调整大小等的图像操纵器:

import { ImageManipulator } from 'expo';
Run Code Online (Sandbox Code Playgroud)

...

const manipResult = await ImageManipulator.manipulate(
    imageUri,
    [{ resize: { width: 640, height: 480 } }],
    { format: 'jpg' }
);
Run Code Online (Sandbox Code Playgroud)

manipResult 将返回一个具有新 uri、宽度和高度的对象。

在这里找到:https : //docs.expo.io/versions/latest/sdk/imagemanipulator.html