如何将图像 uri 转换为 base64?

how can i convert image uri to base64?

我正在使用 react-native-image-picker along with react-native-photo-editor ..

照片编辑器returns编辑图片的uri,而我的api支持base64, 有什么方法可以将编辑后的图像 uri 转换为 base64 编码吗?

下面是我的代码

const handleOnUploadFromLibrary = () => {
    launchImageLibrary(libraryOptions, (response: ImagePickerResponse) => {
    if (response.assets) {
        let assets = response.assets[0];
        let imagePathMinusExtention = assets.uri?.replace('file://', '');
        setImagePickerAsset({...assets, uri: imagePathMinusExtention});
      }
      PhotoEditor.Edit({
        path: imagePickerAsset?.uri || '',
        onDone: (path) => {
          console.log('donee::::');
          let extention = "file://";
          let imagePathPlusExtention = extention.concat(`${path}`);
          setImagePickerAsset({...imagePickerAsset, uri: imagePathPlusExtention});

          var data: UsedAttachment = {
            fileType: imagePickerAsset?.type,
            fileName: imagePickerAsset?.fileName,
            refId: visit.draftAppRequestId,
            refTypeId: '5', //draftx
            title: imagePickerAsset?.fileName,
            mimeType: imagePickerAsset?.type,
            modelMapperKey: '',
            files: [imagePickerAsset?.uri],
          };

          callUploadAttachment({data});

          hideDialog();
        },
        onCancel: () => {
          console.log('Cancel ::: ');
          hideDialog();
        },
      });
    });
 };

如果您使用的是 react-native cli,则可以使用 react-native-fs

文档:https://github.com/itinance/react-native-fs#readfilefilepath-string-encoding-string-promisestring

import RNFS from 'react-native-fs';

//base64 res
var data = await RNFS.readFile( "file://path-to-file", 'base64').then(res => { return res });