Image 如何在react native中保存、显示和图像?

Image 如何在react native中保存、显示和图像?,image,react-native,store,Image,React Native,Store,我有个问题要问。我正在使用一个名为“react native image picker”(反应本机图像选取器)的模块来选取图像并将其显示在我的应用程序上 现在我想要的是将它存储在某个地方(数据库或本地存储),当我再次打开应用程序时,我选择的图像应该在那里。但是我不知道什么是最好的选择 我已经试着读一些东西,比如react native fs和fetch blob,但我想这对我没有帮助 最好的选择是什么 谢谢。首先,根据条件渲染视图。例如,如果图像可用,则只需显示图像,否则显示TouchableO

我有个问题要问。我正在使用一个名为“react native image picker”(反应本机图像选取器)的模块来选取图像并将其显示在我的应用程序上

现在我想要的是将它存储在某个地方(数据库或本地存储),当我再次打开应用程序时,我选择的图像应该在那里。但是我不知道什么是最好的选择

我已经试着读一些东西,比如react native fs和fetch blob,但我想这对我没有帮助

最好的选择是什么


谢谢。

首先,根据条件渲染视图。例如,如果图像可用,则只需显示图像,否则显示TouchableOpacity,这将有助于选择图片:

导入React,{Component}来自React;
从“react native”导入{View,TouchableOpacity,Text,Image};
从“react native image picker”导入ImagePicker;
从“@react native community/async storage”导入异步存储;
类应用程序扩展组件{
建造师(道具){
超级(道具);
此.state={
isImageAvailable:false,
profilePic:null
}
}
componentDidMount=()=>{
这是getImage();
}
getImage=async()=>{
const profilePic=await AsyncStorage.getItem(“profilePic”);
如果(图片){
这是我的国家({
isImageAvailable:对,
profilePic:JSON.parse(profilePic)
});
}
}
选择ProfilePic=()=>{
常量选项={
标题:“选择化身”,
存储选项:{
skipBackup:没错,
路径:'图像',
},
};
ImagePicker.showImagePicker(选项,(响应)=>{
log('Response=',Response);
if(response.didconcel){
log('User cancelled image picker');
}else if(response.error){
log('ImagePicker错误:',response.Error);
}else if(response.customButton){
console.log('用户点击自定义按钮:',response.customButton);
}否则{
const source={uri:response.uri};
//您还可以使用以下数据显示图像:
//const source={uri:'data:image/jpeg;base64'+response.data};
AsyncStorage.setItem(“profilePic”,JSON.stringify(source));
这是我的国家({
资料来源:,
isImageAvailable:正确
});
}
});
}
render(){
返回(
{
this.state.isImageAvailable&&(
)
}
{
!this.state.isImageAvailable&&(
选择个人资料图片
)
}
)
}
} 

希望它能帮助你

您可以将其用作Asyncstorage的本地存储。

将结果值保存到本地存储并检索存储在存储上的数据。嘿,为什么要将
wait
用于Asyncstorage?因为它是异步函数。