如何在React Native中获取base64的图像

时间:2016-05-08 11:46:40

标签: base64 react-native

我使用反应原生图像选择器和图像缩放器来挑选然后调整图像大小。如何在调整图像大小后获取图像的base64?

ImagePickerManager.showImagePicker(imagepicker_options, (response) => {
  ImageResizer.createResizedImage(response.uri, 550, null, 'JPEG', 100).then((resizedImageUri) => {
    //get base64 of image
  });
});

2 个答案:

答案 0 :(得分:4)

https://libraries.io/npm/react-native-asset-library-to-base64

ImagePickerManager.showImagePicker(imagepicker_options, (response) => {
  ImageResizer.createResizedImage(response.uri, 550, null, 'JPEG', 100).then((resizedImageUri) => {
      //get base64 of image, uri is link to asset-library://
      ReadImageData.readImage(uri, (imageBase64) => {
    console.log(imageBase64);
    });
  });
});

如果您没有https://github.com/facebook/react-native/issues/1158

,也可能需要阅读此内容

答案 1 :(得分:0)

实际上,imagepicker_options提供了一种调整大小的方法。 您可以在选项上传递maxWidthmaxHeight

代码:

const imagepicker_options = {
  mediaType: 'photo',
  maxWidth: 550,
  storageOptions: {
    skipBackup: true,
    cameraRoll: false,
    path: 'images',
  },
}
ImagePickerManager.showImagePicker(imagepicker_options, (response) => {
  //get base64 of image
  const base64 = response.data
});