Skip to content

Commit e22aa57

Browse files
authored
feat(clearImageCache): add ability to remove single image cache (#192)
* feat(clearImageCache): add ability to remove single image cache * Update README.md
1 parent 880e803 commit e22aa57

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,22 @@ class MyComponent {
338338
339339
```
340340
341+
# Clearing single image cache
342+
```typescript
343+
344+
import { ImageLoader } from 'ionic-image-loader';
345+
346+
@Component(...)
347+
class MyComponent {
348+
349+
constructor(imageLoader: ImageLoader) {
350+
imageLoader.clearImageCache('http://path.to/image.jpeg');
351+
}
352+
353+
}
354+
355+
```
356+
341357
# Passing HTML / CSS Attributes to a generated image
342358
343359
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.

src/providers/image-loader.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,42 @@ export class ImageLoader {
138138
return this.file.cacheDirectory;
139139
}
140140

141+
/**
142+
* Clears cache of a single image
143+
* @param {string} imageUrl Image URL
144+
*/
145+
clearImageCache(imageUrl: string): void {
146+
if (!this.platform.is('cordova')) {
147+
return;
148+
}
149+
const clear = () => {
150+
if (!this.isInit) {
151+
// do not run this method until our service is initialized
152+
setTimeout(clear.bind(this), 500);
153+
return;
154+
}
155+
const fileName = this.createFileName(imageUrl);
156+
const route = this.getFileCacheDirectory() + this.config.cacheDirectoryName;
157+
// pause any operations
158+
this.isInit = false;
159+
this.file.removeFile(route, fileName)
160+
.then(() => {
161+
if (this.isWKWebView && !this.isIonicWKWebView) {
162+
this.file.removeFile(this.file.tempDirectory + this.config.cacheDirectoryName, fileName)
163+
.then(() => {
164+
this.initCache(true);
165+
}).catch(err => {
166+
//Handle error?
167+
})
168+
} else {
169+
this.initCache(true);
170+
}
171+
}).catch(this.throwError.bind(this));
172+
};
173+
clear();
174+
}
175+
176+
141177
/**
142178
* Clears the cache
143179
*/

0 commit comments

Comments
 (0)