2017-09-18 79 views

回答

0

更新澄清:React Native不公开此方法以执行您现在需要的方法,您将不得不使用第三方解决方案。

因此,您可以使用我的高阶组件模块升级<图片>来代替使用本机Image.prefetch()来处理缓存/永久存储,并完成此操作。

React Native Image Cache HOC

铊; DR代码示例:

import imageCacheHoc from 'react-native-image-cache-hoc'; 
const CacheableImage = imageCacheHoc(Image); 

export default class App extends Component<{}> { 
    render() { 
    return (
     <View style={styles.container}> 
     <Text style={styles.welcome}>Welcome to React Native!</Text> 
     <CacheableImage style={styles.image} source={{uri: 'https://i.redd.it/rc29s4bz61uz.png'}} /> 
     <CacheableImage style={styles.image} source={{uri: 'https://i.redd.it/hhhim0kc5swz.jpg'}} permanent={true} /> 
     </View> 
); 
    } 
} 

第一图像将被缓存,直到总本地缓存增长过去15 MB(默认),则高速缓存的图像被首先删除最老直到总缓存再次低于15 MB。

第二张图像将永久存储到本地磁盘。人们使用这个替代品来替代您的应用发布静态图片文件。

如果你想预暖缓存样Image.prefetch()

import imageCacheHoc from 'react-native-image-cache-hoc'; 
const CacheableImage = imageCacheHoc(Image); 
CacheableImage.cacheFile('https://i.redd.it/hhhim0kc5swz.jpg') 
    .then(localFileInfo => { 
    console.log(localFileInfo); 

    // Console log outputs: 
    //{ 
    // url: 'https://i.redd.it/rc29s4bz61uz.png', 
    // cacheType: 'cache', //add true as 2nd param to cacheFile() to make permanent 
    // localFilePath: '/this/is/absolute/path/to/file.jpg' 
    //} 

    }); 

然后,当你想从磁盘清除它的要求:

import RNFetchBlob from 'react-native-fetch-blob'; 
RNFetchBlob.fs.unlink(localFilePath.localFilePath) 
    .then(() => { 
    console.log('file deleted!'); 
    }); 

希望这可以帮助你!

+0

不回答问题,而是说“使用此”。 –

+0

@AdamBarnes这是因为答案是“你不能做你想用标准React Native做的事情。”我已经更新了我的回答以澄清。 – billmalarky

+0

Downvote删除了,谢谢。 –