如何提高图像下载速度?

时间:2017-10-02 17:19:38

标签: react-native

我正在使用react-native-fetch-blob下载图片。

这是我的源代码。

downloadThumb(element) {
    return new Promise((resolve, reject) => {
      RNFetchBlob.config({
        // add this option that makes response data to be stored as a file,
        // this is much more performant.
        fileCache: true,
        path: `${DocumentDirectoryPath}/thumbs/${element}`,
      })
        .fetch(
          'GET',
          `https://www.searchforsites.co.uk/images/thumbs/${element}`,
          {
            // some headers ..
          },
        )
        .then((res) => {
          resolve(true);
        })
        .catch((err) => {
          // console.log('Error: ', err);
          // this.setState({ modalOpen: false, regionUpdated: true });
          // this.findUser();
          reject(true);
        });
    });
  }

///////////////////////////////////////
  recursive_thumb(element) {
    this.downloadThumb(element).then((response) => {
      if (this.state.count > this.state.total_thumbs) {
        this.setState({ modalOpen: false });
        setThumbDownloaded();
        return true;
      }
      this.state.count += 1;
      // console.log(this.state.count);
      this.setState({ totalCount: this.state.total_thumbs, fetchedCount: this.state.count });
      this.recursive_thumb(this.state.thumbs[this.state.count]);
    });
  }

filesize大约是8KB。现在我一个接一个地下载。 下载一张图片(8KB)需要一秒钟。

有没有提高下载速度的解决方案?

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

如果您想增加一系列图片的下载量: 只需创建一个Promise.all,它将全部并行获取

Promise.all([
    downloadThumb('data.jpeg'),
    downloadThumb('users.jpeg'),
    downloadThumb('products.jpeg')
])
.then(function(data) {
    console.log('Parallel promises >>>', data);
});

查看此github页面:https://github.com/jaydson/es7-async

相关问题