React Native에서 이미지를 캐시하는 방법

Nov 29 2022
캐시에 저장된 이미지를 보여드리겠습니다. 우리는 이미지 저장을 위해 이 라이브러리를 사용할 것입니다: react-native-fs.

캐시에 저장된 이미지를 보여드리겠습니다. 이미지 저장을 위해 react-native-fs 라이브러리를 사용할 것 입니다.

파일 스트림 사용을 시작하려면 먼저 react-native-fs를 가져옵니다 .

import RNFS from 'react-native-fs';

let CacheDir = RNFS.CachesDirectoryPath + '/';

{"bytesWritten": 0, "jobId": 1, "statusCode": 200}

function saveImageInCache(imageUrl, imageName) {
    let filePath = CacheDir + imageName;

    RNFS.downloadFile({
        fromUrl: imageUrl,
        toFile: filePath,
    }).promise.then(response => {
        if(response.statusCode == 200)
            return filePath;
    })
}

async function isExistImage(imagePath) {
            await RNFS.exists(imagePath)
                .then((exists) => {
                    return exists;
                });
        }