在 react native 中将图像转换为 base64



我使用了 react-native-image-picker,现在我从照片库中选择图像。要将该图像发送到API,我必须首先将其转换为base64,然后再转换为字节数组。我在 response.uri 中有文件路径。我该怎么做?

当我做控制台.log(响应(时,我得到了这个结果

'Response = ', {
  fileSize: 6581432,
  longitude: -17.548928333333333,
  uri: 'file:///Users/shubhamb/Library/Developer/CoreSimulator/Devices/B58314DF-F0A9-48D2-B68A-984A02271B72/data/Containers/Data/Application/63143214-8A03-4AC8-A79C-42EC9B82E841/tmp/2AACBC57-0C07-4C98-985E-154668E6A384.jpg',
  fileName: 'IMG_0003.JPG',
  latitude: 65.682895,
  origURL: 'assets-library://asset/asset.JPG?id=9F983DBA-EC35-42B8-8773-B597CF782EDD&ext=JPG',
  type: 'image/jpeg',
  height: 2002,
  width: 3000,
  timestamp: '2012-08-08T18:52:11Z',
  isVertical: false,
}

由于您使用的是 react-native-image-picker,因此它已经在其响应中返回了 Base64 值

ImagePicker.showImagePicker(options, (response) => {
  const base64Value = response.data;
});

响应的文档

我在

更新应用程序时突然遇到了这个问题。我发现以前的react-native-image-picker用于提供base64作为response.data。但是现在选项对象中有一个includeBase64,以便您可以控制是否需要base64数据。所以我的代码变成了下面这样

captureTradeLicenseImage() {
    let options = {
        maxHeight: 250,
        maxWidth: 350,
        includeBase64: true //add this in the option to include base64 value in the response
    }
    ImagePicker.launchCamera(options, (response)  => {
        console.log('Response = ', response)
        if (response.didCancel) {
            console.log('User cancelled image picker')
        }
        else if (response.error) {
            console.log('ImagePicker Error: ', response.error)
        }
        else if (response.customButton) {
            console.log('User tapped custom button: ', response.customButton)
        }
        else if (response.fileSize > 5242880) {
            Alert.alert(
                "Nilamhut Say's",
                "Oops! the photos are too big. Max photo size is 4MB per photo. Please reduce the resolution or file size and retry",
                [
                    { text: "OK", onPress: () => console.log('ok Pressed') }
                ],
                { cancelable: false }
            )
        }
        else {
            this.setState({tradeLicenseImageData: response.base64}) //access like this
        }
    })
}

独立的 expo 文件系统包使这变得简单:

const base64 = await FileSystem.readAsStringAsync(photo.uri, { encoding: 'base64' });
  • https://docs.expo.io/versions/latest/sdk/filesystem/
  • https://github.com/expo/expo/tree/master/packages/expo-file-system

作为 2019-09-27,此包处理file://content:// uri

我为时已晚,但是如果我可以帮助其他人,那就是寻找如何从图像中获取base64数据:在选项对象中,您必须将 base64 选项设置为 true,如下所示:

const options = {
   title: 'Choose an Image',
   base64: true
};
ImagePicker.launchImageLibrary(options, response => {
    console.log(response.data);
});

就这么简单:

import { CameraOptions, ImagePickerResponse, launchCamera } from 'react-native-image-picker';
Wrap in your component: 
const [b64, setB64] = useState<string>("");
const manageImage = async (response: ImagePickerResponse) => {
    if (response.didCancel) {
      return;
    } else if (response.assets) {
        if (response.assets?.length > 0) {
          setB64(response.assets[0].base64 ? response.assets[0].base64 : "");
        }
    }
  }
launchCamera(options, (response) => {
  manageImage(response);
});

包括

includeBase64: true

在选项中确实为我返回了 base64 字符串。

相关内容

  • 没有找到相关文章

最新更新