Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(clearImageCache): add ability to remove single image cache #192

Merged
merged 2 commits into from
Sep 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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