我有静态图像,需要将其转换为 Base64
,然后将其发送到 Android/iOS native 代码。
如果我从文件中选择图像,我可以将其发送到 native 代码并将其转换为 Base64
。
但是如果我有静态图像然后将其发送到 native 代码怎么办?
<Image source={require('./img/icon.png')}/>
我希望在 Android/iOS native 模块中发送 icon.png
。
我已经完成了 native 编码,是这样的
@ReactMethod
public void filterBase64(String base64, Callback stringCallback) {
}
但卡在如何发送Base64
我查过react-native-image-to-base64但当我使用时无法得到解决方案
NativeModules.RNImageToBase64.getBase64String(uri, (err, base64) => {
// Do something with the base64 string
})
它显示错误未定义不是对象
最佳答案
在RNFS的帮助下通过插件,您可以访问 React Native 资源并将其转换为 Base64。
var RNFS = require('react-native-fs')
base64data = await RNFS.readFile('./img/icon.png', 'base64').then();
console.log(base64data);
关于android - 在 React Native 中将静态图像转换为 Base64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43338242/