ios 保存:
React Native 的 CameraRoll API 提供了訪問本地相冊(cè)的功能(經(jīng)過測(cè)試該組件只支持 ios)
import {Platform,CameraRoll} from 'react-native';
var promise = CameraRoll.saveToCameraRoll("圖片的 url")
promise.then(function(result) {
this.refs.toast.show("圖片已保存至相冊(cè)")
}).catch(function(error) {
this.refs.toast.show("保存失敗")
})
Android 的保存利用 react-native-fs 進(jìn)行保存圖片
const RNFS = require('react-native-fs') //文件處理
const storeLocation = `${RNFS.DocumentDirectoryPath}`
// 安卓上傳圖片
function sendImgAndroid(url,succeedFun,errerFn){
console.log(url)
let pathName = new Date().getTime() + "xg.png"
let downloadDest = `${storeLocation}/${pathName}`;
const ret = RNFS.downloadFile({fromUrl:url,toFile:downloadDest});
ret.promise.then(res => {
if(res && res.statusCode === 200){
var promise = CameraRoll.saveToCameraRoll("file://" + downloadDest);
promise.then(function(result) {
// 保存成功
console.log('圖片保存成功')
succeedFun(result)
}).catch(function(error) {
// 保存失敗
console.log('圖片保存失敗')
errerFn(error)
})
}
})
}