首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >离子2缓存映像

离子2缓存映像
EN

Stack Overflow用户
提问于 2017-02-15 09:55:56
回答 2查看 4.2K关注 0票数 2

我正在编写一个离子2应用程序,并希望缓存图像。

在网上搜索了很长时间之后,我找到了以下参考资料:https://gist.github.com/ozexpert/d95677e1fe044e6173ef59840c9c484e

https://github.com/chrisben/imgcache.js/blob/master/js/imgcache.js

我实现了给定的解决方案,但我看到ImgCache模块的行为并不像预期的那样--回调从未被调用过。

对于在离子2中缓存映像,有什么想法或其他好的解决方案吗?

========更新==========

下面是我使用的指令代码:

代码语言:javascript
复制
import { Directive, ElementRef, Input } from '@angular/core';

import ImgCache from 'imgcache.js';

@Directive({
  selector: '[image-cache]'
})
export class ImageCacheDirective {
  constructor (
    private el: ElementRef
  ) {
    // init
  }

  ngOnInit() {
    // This message is shown in console
    console.log('ImageCacheDirective *** ngOnInit: ', this.el.nativeElement.src);

    this.el.nativeElement.crossOrigin = "Anonymous"; // CORS enabling

    ImgCache.isCached(this.el.nativeElement.src, (path: string, success: any) => {
      // These message are never printed
      console.log('path - '+ path);
      console.log('success - '+ success);

      if (success) {
        // already cached
        console.log('already cached so using cached');

        ImgCache.useCachedFile(this.el.nativeElement);
      } else {
        // not there, need to cache the image
        console.log('not there, need to cache the image - ' + this.el.nativeElement.src);

        ImgCache.cacheFile(this.el.nativeElement.src, () => {
          console.log('cached file');

          // ImgCache.useCachedFile(el.nativeElement);
        });
      }
    });
  }
}

app.nodule.es中,我需要:

代码语言:javascript
复制
import { ImageCacheDirective } from '../components/image-cache-directive/image-cache-directive';

然后在home.html中:

代码语言:javascript
复制
<img src="http://localhost/ionic-test/img/timeimg.php" image-cache>
EN

回答 2

Stack Overflow用户

发布于 2017-03-27 15:48:48

为时已晚,但这可能是解决办法:

1.安装cordova FileTransfer:

代码语言:javascript
复制
ionic plugin add cordova-plugin-file-transfer --save

2.当科多瓦的设备读取事件触发时,Init ImgCache .在src/app/app.component.ts中,添加这些方法(或将它们与您的initializeApp()方法集成--该方法提供了一个默认的项目启动):

代码语言:javascript
复制
initImgCache() {
    // activated debug mode
    ImgCache.options.debug = true;
    ImgCache.options.chromeQuota = 100 * 1024 * 1024; // 100 MB
    ImgCache.init(() => { },
        () => { console.log('ImgCache init: error! Check the log for errors'); });
}

initializeApp() {
    this.platform.ready().then(() => {
        this.initImgCache();

        // Okay, so the platform is ready and our plugins are available.
        // Here you can do any higher level native things you might need.
        StatusBar.styleDefault();
        Splashscreen.hide();
    });
}
票数 0
EN

Stack Overflow用户

发布于 2017-05-23 10:42:59

另一种选择是使用专用的离子缓存管理器。而不是自己实现一切。

下面是两个选项: 1.通用缓存实现:https://github.com/Nodonisko/ionic-cache 2。

编辑:这不是“只链接”的答案。它告诉用户使用现成的实现,而不是从头开始实现。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42245987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档