react native 图片保存到本地

962 阅读1分钟

环境参数:

  "dependencies": {
    "@ant-design/react-native": "^4.0.0",
    "@react-native-community/async-storage": "^1.11.0",
    "@react-native-community/masked-view": "^0.1.10",
    "@react-navigation/native": "^5.6.1",
    "@react-navigation/stack": "^5.6.2",
    "axios": "^0.19.2",
    "babel-plugin-import": "^1.13.0",
    "react": "16.11.0",
    "react-native": "0.62.2",
    "react-native-flexi-radio-button": "^0.2.2",
    "react-native-fs": "^2.16.6",
    "react-native-gesture-handler": "^1.6.1",
    "react-native-image-crop-picker": "^0.24.1",
    "react-native-image-picker": "^2.3.2",
    "react-native-image-viewing": "^0.2.0",
    "react-native-picker": "^4.3.7",
    "react-native-reanimated": "^1.9.0",
    "react-native-safe-area-context": "^3.0.7",
    "react-native-screens": "^2.9.0",
    "react-native-storage": "^1.0.1",
    "react-native-swiper": "^1.6.0",
    "react-native-vector-icons": "^7.0.0",
    "react-redux": "^7.2.0",
    "redux": "^4.0.5",
    "redux-thunk": "^2.3.0"
  },

安装插件  github.com/itinance/re…

yarn add  react-native-fs --save

import {CameraRoll} from "react-native"
import RNFS from 'react-native-fs';

<TouchableOpacity
       onPress={() => {
                if (Platform.OS === 'ios') {
                    CameraRoll.saveToCameraRoll(this.state.modalImage)
                        .then(res => {
                            Toast("图片保存成功", 2000)
                            this.closeModal()
                        }).catch(error => {
                        console.log(error.response)
                    })
                } else {
                    const downloadDest = `${RNFS.DocumentDirectoryPath}/${((Math.random() * 1000) | 0)}.jpg`;
                    const ret = RNFS.downloadFile({
                        fromUrl: this.state.modalImage,
                        toFile: downloadDest
                    })
                    ret.promise.then(res => {
                        console.log(res, "11111111")
                        if (res && res.statusCode === 200) {
                            CameraRoll.saveToCameraRoll("file://" + downloadDest)
                                .then(res => {
                                    Toast("图片保存成功", 2000)
                                    this.closeModal()
                                }).catch((error) => {
                                console.log('error:', error);
                            })
                        }
                    }).catch(error => {
                        console.log(error)
                    })

                }
            }}> 
       <Text>保存图片</Text>
       </TouchableOpacity>

hooks写法:

<TouchableOpacity
                  onPress={() => {
                    if (Platform.OS === 'ios') {
                      CameraRoll.saveToCameraRoll(configData.qrCode)
                        .then(res => {
                          Alert.alert('图片保存成功')
                          //this.closeModal()
                        }).catch(error => {
                        Alert.alert('图片保存失败')
                        console.log(error.response)
                      })
                    } else {
                      const downloadDest = `${RNFS.DocumentDirectoryPath}/${((Math.random() * 1000) | 0)}.jpg`;
                      console.log(downloadDest,configData)
                      const ret = RNFS.downloadFile({
                        fromUrl: configData.qrCode,
                        toFile: downloadDest
                      })
                      ret.promise.then(res => {
                        console.log(res, "11111111")
                        if (res && res.statusCode === 200) {
                          CameraRoll.saveToCameraRoll("file://" + downloadDest)
                            .then(res => {
                              Alert.alert('图片保存成功')
                              //this.closeModal()
                            }).catch((error) => {
                            Alert.alert('图片保存失败')
                            console.log('error:', error);
                          })
                        }
                      }).catch(error => {
                        Alert.alert('图片保存失败')
                        console.log(error)
                      })

                    }
                  }}>
                  <Text>点击保存</Text>
                </TouchableOpacity>