React-Native: Convert image url to base64 string

前端 未结 9 547
轻奢々
轻奢々 2020-12-08 19:03

I\'m building a react native app that needs to store images at base64 string format for offline viewing capabilities.

What library / function would give me the best

9条回答
  •  暖寄归人
    2020-12-08 19:44

    To convert image to base64 in React native, the FileReader utility is helpful:

    const fileReader = new FileReader();
    fileReader.onload = fileLoadedEvent => {
      const base64Image = fileLoadedEvent.target.result;
    };
    fileReader.readAsDataURL(imagepath); 
    

    This requires react-native-file.

    Another alternative, and probably the preferred alternative, is to use NativeModules. The Medium article shows how. It requires creating a native module.

    NativeModules.ReadImageData.readImage(path, (base64Image) => {
      // Do something here.
    });
    

提交回复
热议问题