Skip to content

Commit

Permalink
feat(clearImageCache): add ability to remove single image cache (#192)
Browse files Browse the repository at this point in the history
* feat(clearImageCache): add ability to remove single image cache

* Update README.md
  • Loading branch information
madoBaker authored Sep 19, 2018
1 parent 880e803 commit e22aa57
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<img>` 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 `<img>` element in the DOM.
Expand Down
36 changes: 36 additions & 0 deletions src/providers/image-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit e22aa57

Please sign in to comment.