gpt4 book ai didi

react-native - 如何在 AsyncStorage 中存储图像?

转载 作者:行者123 更新时间:2023-12-02 19:52:34 32 4
gpt4 key购买 nike

我正在尝试将图片存储在 AsyncStorage 中,但我不知道该怎么做,我应该存储 uri 还是路径?

我试过的是一些废话,但我试过这个:

state = {
avatarSource: null,
avatarSource2: null,
}

constructor(props) {
super(props);
this.state = {
modalVisible: false,
images: [{
url: '',
}],
}
this.setModalVisible = this.setModalVisible.bind(this)
}

saveData() {
AsyncStorage.getItem("uri");
}

然后当我选择我的图像时使用这个:

selectImage = async () => {
ImagePicker.showImagePicker({ nodata: true, mediaType: 'photo' }, (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 {

// You can also display the image using data:
// const source = { uri: 'data:image/jpeg;base64,' + response.data };

this.setState({
avatarSource: response.uri,
});
this.saveData; //here is where i save the data...
}
});
}

然后当我加载页面时...

componentWillMount() {
AsyncStorage.getItem('uri')


}

而我的渲染是这样的...

render() {
return (
<ScrollView>
<View style={styles.container}>
<View style={styles.avatarContainer}>
<TouchableOpacity onPress={() => {
this.setModalVisible(!this.state.modalVisible, this.state.avatarSource);
}}>
{
this.state.avatarSource && <Image source={{ uri: this.state.avatarSource }} style={styles.avatar} />
}
</TouchableOpacity>
</View>
<Button title="Seleciona a imagem" onPress={this.selectImage} />
<View style={styles.avatarContainer}>
<TouchableOpacity onPress={() => {
this.setModalVisible(!this.state.modalVisible, this.state.avatarSource2);
}}>
{
this.state.avatarSource2 && <Image source={{ uri: this.state.avatarSource2 }} style={styles.avatar} />
}
</TouchableOpacity>
</View>
<Button title="Seleciona a imagem" onPress={this.selectImage2} />
</View>
<Modal style={styles.modalImage}
animationType='slide'
transparent={false}
visible={this.state.modalVisible}>
<TouchableHighlight style={{ backgroundColor: 'black' }} onPress={() => {
this.setModalVisible(!this.state.modalVisible) + this.props.navigation.navigate('Cartão de Cidadão');
}}>
<Icon active name="close" size={30} style={{ textAlign: 'center', marginTop: 0, color: 'white' }} />
</TouchableHighlight>
<ImageViewer imageUrls={this.state.images} />
</Modal>
</ScrollView>
);
}

我做错了什么?谢谢。

最佳答案

Async Storage 只能保存字符串。您可以将照片保存到文件系统。尝试使用这个 npm module

关于react-native - 如何在 AsyncStorage 中存储图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57814452/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com