保存到AsyncStorage react-native

时间:2018-08-02 23:16:52

标签: react-native

我在项目中为用户拍照,并将其保存在AsyncStorage中 如何在onPress()按钮中执行此操作? 不要将拍摄的照片保存在用户的移动图库中 例如,以.abc格式 未显示在图库中 以及如何保存typeSlb数据? 该程序运行没有问题 但是我不知道如何添加这些功能?

ScanPhoto.js

export default class ScanPhoto extends Component{
    constructor(props) {
        super(props);
        const { navigation } = this.props;
        this.currentDate = navigation.getParam('currentDate', "0001/01/01");
        this.state = {
            user: {
                ImageSource: null,
                typeSlb: ''}};
    AsyncStorage.getItem('user:ImageSource', (error, result) => {
        let data = [];
        if (result) {
            data = JSON.parse(result);
        }
        let image = data.find(item => item.date === this.currentDate);
        if(image){
            this.setState({
                ImageSource: image.img}); }});}
         selectPhotoTapped() {
                const options = {
                    quality: 1.0,
                    storageOptions: {
                        skipBackup: true}};

    ImagePicker.showImagePicker(options, (response) => {
        console.log('Response = ', response);
        if (response.didCancel) {
            console.log('User cancelled photo picker');}
        else if (response.error) {
            console.log('ImagePicker Error: ', response.error);}
        else if (response.customButton) {console.log('User tapped custom button: ', response.customButton);}
        else {
            let source = { uri: response.uri };
            AsyncStorage.getItem('user:ImageSource', (error, result) => {
                let data = [];
                if (result) {
                    data = JSON.parse(result);}
                let image = data.find(item => item.date === this.currentDate);
                if(image){
                    image.img = source;
                } else {
                    data.push({
                        img: source,
                        date: this.currentDate});}
                AsyncStorage.setItem('user:ImageSource', JSON.stringify(data));});
            this.setState({ImageSource: source});}});}


    render() {
        return (
            <View style={styles.container}>
                <TouchableOpacity onPress={this.selectPhotoTapped.bind(this)}>
                    <View style={styles.ImageContainer}>
                    { this.state.ImageSource === null ? <Text>Select a Photo</Text> :
                    <Image style={styles.ImageContainer} source={this.state.ImageSource} /> }
                    </View></TouchableOpacity>
                        <View><Form>
                                <Item floatingLabel>
                                <Label>type</Label>
                        <Input value={this.state.typeSlb}/></Item>
            <Button rounded light onPress={}><Text> save </Text></Button>

home.js

  constructor(props) {
        super(props);
        this.state = {
            selectedStartDate: null,};
        this.onDateChange = this.onDateChange.bind(this);}
    onDateChange(date) {
        let currentDate = date.format('jYYYY/jM/jD');
        this.props.navigation.navigate('ScanPhoto', {currentDate: currentDate});}

0 个答案:

没有答案