我正在写一个离子2应用程序,并希望缓存图像。离子2缓存图像
后在网络上长期搜索,我发现这些引用: https://gist.github.com/ozexpert/d95677e1fe044e6173ef59840c9c484e
https://github.com/chrisben/imgcache.js/blob/master/js/imgcache.js
我实现给定的解决方案,但我认为这是预期ImgCache模块不表现 - 的ImgCache.isCached
回调从未打过电话
任何想法或其他好的解决方案缓存图像在离子2?
======== UPDATE ==========
这里是指令代码,我使用:
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我做的:
import { ImageCacheDirective } from '../components/image-cache-directive/image-cache-directive';
,然后在Home.html中:
<img src="http://localhost/ionic-test/img/timeimg.php" image-cache>
请在声明/使用模块的位置添加代码部分 – akz92
我不能相信图像缓存被离子社区忽视.. – EralpB