我正在编写一个离子2应用程序,并希望缓存图像。
在网上搜索了很长时间之后,我找到了以下参考资料:https://gist.github.com/ozexpert/d95677e1fe044e6173ef59840c9c484e
https://github.com/chrisben/imgcache.js/blob/master/js/imgcache.js
我实现了给定的解决方案,但我看到ImgCache模块的行为并不像预期的那样--回调从未被调用过。
对于在离子2中缓存映像,有什么想法或其他好的解决方案吗?
========更新==========
下面是我使用的指令代码:
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>发布于 2017-03-27 15:48:48
为时已晚,但这可能是解决办法:
1.安装cordova FileTransfer:
ionic plugin add cordova-plugin-file-transfer --save2.当科多瓦的设备读取事件触发时,Init ImgCache .在src/app/app.component.ts中,添加这些方法(或将它们与您的initializeApp()方法集成--该方法提供了一个默认的项目启动):
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();
});
}发布于 2017-05-23 10:42:59
另一种选择是使用专用的离子缓存管理器。而不是自己实现一切。
下面是两个选项: 1.通用缓存实现:https://github.com/Nodonisko/ionic-cache 2。
编辑:这不是“只链接”的答案。它告诉用户使用现成的实现,而不是从头开始实现。
https://stackoverflow.com/questions/42245987
复制相似问题