react-native-img-cache icon indicating copy to clipboard operation
react-native-img-cache copied to clipboard

How to get image cache size ?

Open chuanfengH opened this issue 8 years ago • 5 comments

chuanfengH avatar Aug 21 '17 14:08 chuanfengH

@chuanfengH Are you referring to image size on disk or the number images in the cache?

antondomratchev avatar Aug 25 '17 20:08 antondomratchev

@antondomratchev image size on disk . before i use ImageCache.get().clear() , i want to get the total cache size.

chuanfengH avatar Sep 13 '17 08:09 chuanfengH

这个问题解决了吗?我也想知道在清空缓存之前,如何显示缓存大小,谢谢!

cyjcxy avatar Sep 22 '17 02:09 cyjcxy

This feature is very useful when user decides to clear the cache.

just4fun avatar Dec 05 '17 03:12 just4fun

It's work for me ^_^


import RNFetchBlob from "rn-fetch-blob";

......

bytesToSize(bytes) {
    if (bytes === 0) {
      return '0 B';
    }

    let k = 1024;

    let sizes = ['B','KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

    let i = Math.floor(Math.log(bytes) / Math.log(k));

    let num = bytes / Math.pow(k, i);
    return num.toPrecision(3) + ' ' + sizes[i];
  }

......

componentDidMount() {
  console.log(RNFetchBlob.fs.dirs.CacheDir + "/react-native-img-cache");
  RNFetchBlob.fs.lstat(RNFetchBlob.fs.dirs.CacheDir + '/react-native-img-cache')
      .then((stats) => {
        console.log(stats);
        let bytes = 0;
        stats.map(f => {
          bytes += Number(f.size);
        });
        console.log(this.bytesToSize(bytes));
      })
      .catch((err) => {
        console.log(err);
      })
}

exculibar avatar Dec 18 '18 14:12 exculibar