【问题标题】:In Ionic Framework (v5), how do you use blob images saved in the data directory as inline and background images?在 Ionic Framework (v5) 中,如何将保存在数据目录中的 blob 图像用作内联图像和背景图像?
【发布时间】:2020-09-14 06:57:13
【问题描述】:

我创建了一个用于下载和保存 blob 图像的函数,这样如果用户离线,图像仍然可以呈现。我必须这样做,因为产品是通过 CMS 管理的。

函数如下:

downloadProductImages(products) {
  return new Promise((resolve, reject) => {
    this.platform.ready()
      .then(() => {
        for (let i = 0; i < products.length; i++) {
          const productImageUrl = SERVER_URL + products[i].imageUrl,
                fileName = products[i].image;
          this.http
            .sendRequest(productImageUrl, {
              method: 'download',
              filePath: this.directoryPath + fileName,
              responseType: 'blob'
            })
            .then((response: any) => {
              this.file.writeFile(this.directory, fileName, response, {replace: true})
                .then(_ => {
                  resolve();
                })
                .catch(error => {
                  reject();
                });
            })
            .catch(error => {
              reject();
            });
        }
      });
  });
}

这是我希望图像呈现的页面视图:

<div [ngStyle]="{'background-image': 'url(\'' + (productImage !== '' ? productImage : '../../assets/images/image-not-available.png' | sanitizeUrl) + '\')'}">
  <ion-row>
    <ion-col size="12" size-sm="6">
      <div class="show-mobile">
        <img [src]="(productImage !== '' ? productImage : '../../assets/images/image-not-available.png' | sanitizeUrl)" class="img-responsive">
      </div>
    </ion-col>
  </ion-row>
</div>

【问题讨论】:

    标签: angular ionic-framework blob ionic4 ionic5


    【解决方案1】:

    浏览器上下文中的文件 API 主要围绕“读取”用例构建。将文件写入客户端计算机会受到安全问题的影响,并且在客户端没有无缝的 API 来执行此操作。

    所以你可以在这里采取的方法是:

    • 使用 Ionic Storage 存储图像的 blob(localForage 确实支持在 indexeddb 中存储 blob)
    • 在应用程序启动时检查存储的 blob 并通过运行 forEach 循环恢复本地缓存并创建 blobUrls
    • 为您的 img 元素创建条件,以便在应用离线时从本地 blobUrl 读取

    如下所示的方向将是您的主要功能:

    async cacheImagesLocally() {
    
        // not sure what your products array looks like, but just for example here:
        const products = [{
          image: "image0",
          imageUrl: "someURL"
        }];
    
        // this one should probably be a property in your class:
        const localBlobUrlsCache = [];
    
        for (const product of products) {
    
          let localBlob = await this.storage.get(product.image);
    
          if (!localBlob) {
    
            const productImageUrl = SERVER_URL + product.imageUrl;
            const fileName = product.image;
    
            localBlob = await this.http.sendRequest(productImageUrl, {
              method: 'download',
              filePath: this.directoryPath + fileName,
              responseType: 'blob'
            });
    
          };
    
          localBlobUrlsCache.push({image: product.image, blobUrl: URL.createObjectURL(localBlob)});
    
        };
    
      };
    

    【讨论】:

    • 感谢您的建议。我会看看我是否可以在我的应用中使用类似的东西,我会告诉你它是否适合我。
    • 酷。我在我的应用程序中使用了这样的缓存,它在跨浏览器上运行得非常好。在我的情况下,这也减少了服务器上的下载请求数量并提供更好的离线首次体验
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    • 2014-10-05
    • 2016-09-01
    • 1970-01-01
    • 2017-02-19
    • 2013-06-20
    相关资源
    最近更新 更多