React Native双系统图片下载
2019-06-21 本文已影响2人
Volon
使用的插件:
1.IOS端:ReactNative API中的CameraRoll
2.Android端:react-native-fs
引入方法:
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>