diff --git a/README.md b/README.md index b13ce87..e92e7bf 100644 --- a/README.md +++ b/README.md @@ -338,6 +338,22 @@ class MyComponent { ``` +# Clearing single image cache +```typescript + +import { ImageLoader } from 'ionic-image-loader'; + +@Component(...) +class MyComponent { + + constructor(imageLoader: ImageLoader) { + imageLoader.clearImageCache('http://path.to/image.jpeg'); + } + +} + +``` + # Passing HTML / CSS Attributes to a generated image When using ImageLoader to generate an `` element it may be desirable for the generated element to include additional attributes to provide styling or interaction qualities. The optional `imgAttributes` value can be used to provide such additional attributes which will be included in the generated `` element in the DOM. diff --git a/src/providers/image-loader.ts b/src/providers/image-loader.ts index ccece54..50a46ec 100644 --- a/src/providers/image-loader.ts +++ b/src/providers/image-loader.ts @@ -138,6 +138,42 @@ export class ImageLoader { return this.file.cacheDirectory; } + /** + * Clears cache of a single image + * @param {string} imageUrl Image URL + */ + clearImageCache(imageUrl: string): void { + if (!this.platform.is('cordova')) { + return; + } + const clear = () => { + if (!this.isInit) { + // do not run this method until our service is initialized + setTimeout(clear.bind(this), 500); + return; + } + const fileName = this.createFileName(imageUrl); + const route = this.getFileCacheDirectory() + this.config.cacheDirectoryName; + // pause any operations + this.isInit = false; + this.file.removeFile(route, fileName) + .then(() => { + if (this.isWKWebView && !this.isIonicWKWebView) { + this.file.removeFile(this.file.tempDirectory + this.config.cacheDirectoryName, fileName) + .then(() => { + this.initCache(true); + }).catch(err => { + //Handle error? + }) + } else { + this.initCache(true); + } + }).catch(this.throwError.bind(this)); + }; + clear(); + } + + /** * Clears the cache */