【问题标题】:How to store a image in AsyncStorage?如何将图像存储在 AsyncStorage 中?
【发布时间】:2019-10-02 10:10:59
【问题描述】:

我试图在 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>
    );
}

}

我做错了什么? 谢谢。

【问题讨论】:

  • 你解决了吗?

标签: react-native asyncstorage


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    Easy Fix,当你将它存储到异步并在检索时解析时,你必须 stringfy:

    存储图像选择器:

    AsyncStorage.setItem(@myKey,JSON.stringify(source));
    
    

    检索:

                const value = await AsyncStorage.getItem(@mykey)
                if(value !== null) {
                    this.setState({ image: JSON.parse(value)});
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-15
      • 1970-01-01
      • 2016-05-19
      • 2010-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多