React Native Android]调整图像大小并上载到后端服务器
使用该设备拍摄的照片很大。我想在将它们调整到更合理的大小(小于800x800)后将它们上载到后端服务器。我希望使用React Native Android]调整图像大小并上载到后端服务器,android,react-native,Android,React Native,使用该设备拍摄的照片很大。我想在将它们调整到更合理的大小(小于800x800)后将它们上载到后端服务器。我希望使用ImageEditor模块的coprImage()函数,但使用大图像运行它会导致OutOfMemory异常。我假设,由于模块试图解码一个大图像并将其存储在内存中,因此应用程序崩溃 我需要的是: 输入 输出 { width: 800, height: 650, uri: content://android/1/resized (some location in Androi
ImageEditor
模块的coprImage()
函数,但使用大图像运行它会导致OutOfMemory
异常。我假设,由于模块试图解码一个大图像并将其存储在内存中,因此应用程序崩溃
我需要的是:
输入
输出
{
width: 800,
height: 650,
uri: content://android/1/resized (some location in Android device)
}
然后我可以抓取这个uri将图片发送到我的后端服务器,并从设备中删除调整大小的照片
我假设我必须编写一个NativeModule
,这样我就可以在不将大的解码图像加载到内存的情况下调整图像的大小。React Native的Image
组件用于在渲染图像之前处理大小调整,但我认为它没有提供一种方法来调整图像大小并将其临时保存在fs中
任何帮助都将不胜感激
参考资料:
在我的应用程序中,我使用了一种非常有效的方法
如果您不想使用它,请查看如何调整大小。
干杯。你试过了吗?对我来说,它工作得很好,我正在使用它,它看起来像这样:
fromCamera() {
let newWidth = 800;
let newHeight = 650;
this.refs.camera.capture()
.then((data) => {
ImageResizer.createResizedImage(data.path, newWidth, newHeight, 'JPEG', 100, rotation)
.then(uri => {
// send to backend
}
})
}
原始图像以
uri:data.path保存在设备上,因此内存没有问题 Expo library有一个图像操纵器,可以调整大小和更多:
import { ImageManipulator } from 'expo';
manipResult
将返回具有新uri、宽度和高度的对象
在这里找到它:
让我们结账吧。它将为您提供有关调整大小和上载到后端服务器的问题的详细说明。如果您想发送Base64字符串,可以检查下面的代码
如何安装和链接android请检查此链接
//此代码仅用于调整大小并推送到数组
let images = [];
NativeModules.RNAssetResizeToBase64.assetToResizedBase64(
response.uri,
newWidth,
newHeight,
(err, base64) => {
if (base64 != null) {
const imgSources = { uri: base64 };
this.setState({
image: this.state.image.concat(imgSources)
});
this.state.image.map((item, index) => {
images.push(item);
});
if (err) {
console.log(err, "errors");
}
}
}
);
从哪里获得ImageResizer组件?
const manipResult = await ImageManipulator.manipulate(
imageUri,
[{ resize: { width: 640, height: 480 } }],
{ format: 'jpg' }
);
let images = [];
NativeModules.RNAssetResizeToBase64.assetToResizedBase64(
response.uri,
newWidth,
newHeight,
(err, base64) => {
if (base64 != null) {
const imgSources = { uri: base64 };
this.setState({
image: this.state.image.concat(imgSources)
});
this.state.image.map((item, index) => {
images.push(item);
});
if (err) {
console.log(err, "errors");
}
}
}
);