2017-09-18 6 views

답변

0

업데이트 : 명확한 네이티브가 현재 사용자가 필요로하는 것을 수행하는 방법을 노출하지 않으며, 타사 솔루션을 사용해야합니다.

네이티브 Image.prefetch()를 사용하는 대신 상위 상위 구성 요소 모듈을 사용하여 < 이미지 >을 캐싱/영구 저장 장치를 처리하고 업그레이드 할 수 있습니다.

React Native Image Cache HOC

T1 내지; DR 코드 예 :

총 로컬 캐시는 다음 캐시 된 이미지가 전체 때까지 첫 번째 오래된 삭제됩니다 (기본적으로) 지난 15 MB의 성장까지 첫 번째 이미지는 캐시됩니다
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> 
); 
    } 
} 

캐시가 다시 15MB 미만입니다.

두 번째 이미지는 로컬 디스크에 영구 저장됩니다. 사람들은이 앱을 사용하여 정적 이미지 파일을 제공하는 대체품으로 사용합니다. 당신이 요청에 따라 디스크에서 삭제하고자 할 때

당신이 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가 삭제되었습니다. 감사합니다. –