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

Feature: Add clear image cache from memory and disk #425

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -4,6 +4,7 @@

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
Expand Down Expand Up @@ -53,4 +54,33 @@ public void run() {
}
});
}

@ReactMethod
public void clearMemoryCache(final Promise promise) {
final Activity activity = getCurrentActivity();
if (activity == null) {
promise.resolve(null);
return;
}

activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Glide.get(activity.getApplicationContext()).clearMemory();
promise.resolve(null);
}
});
}

@ReactMethod
public void clearDiskCache(Promise promise) {
final Activity activity = getCurrentActivity();
if (activity == null) {
promise.resolve(null);
return;
}

Glide.get(activity.getApplicationContext()).clearDiskCache();
promise.resolve(null);
}
}
15 changes: 14 additions & 1 deletion ios/FastImage/FFFastImageViewManager.m
@@ -1,6 +1,7 @@
#import "FFFastImageViewManager.h"
#import "FFFastImageView.h"

#import <SDWebImage/SDImageCache.h>
#import <SDWebImage/SDWebImagePrefetcher.h>

@implementation FFFastImageViewManager
Expand Down Expand Up @@ -34,5 +35,17 @@ - (FFFastImageView*)view {
[[SDWebImagePrefetcher sharedImagePrefetcher] prefetchURLs:urls];
}

@end
RCT_EXPORT_METHOD(clearMemoryCache:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
{
[SDImageCache.sharedImageCache clearMemory];
resolve(NULL);
}

RCT_EXPORT_METHOD(clearDiskCache:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
{
[SDImageCache.sharedImageCache clearDiskOnCompletion:^(){
resolve(NULL);
}];
}

@end
2 changes: 2 additions & 0 deletions src/index.js.flow
Expand Up @@ -68,4 +68,6 @@ declare export default class FastImage extends React$Component<FastImageProps> {
static priority: Priority;
static cacheControl: CacheControl;
static preload: PreloadFn;
static clearMemoryCache: () => Promise<void>;
static clearDiskCache: () => Promise<void>;
}
6 changes: 6 additions & 0 deletions src/index.tsx
Expand Up @@ -203,6 +203,8 @@ interface FastImageStaticProperties {
priority: typeof priority
cacheControl: typeof cacheControl
preload: (sources: Source[]) => void
clearMemoryCache: () => Promise<void>
clearDiskCache: () => Promise<void>
}

const FastImage: React.ComponentType<FastImageProps> &
Expand All @@ -217,6 +219,10 @@ FastImage.priority = priority
FastImage.preload = (sources: Source[]) =>
FastImageViewNativeModule.preload(sources)

FastImage.clearMemoryCache = () => FastImageViewNativeModule.clearMemoryCache()

FastImage.clearDiskCache = () => FastImageViewNativeModule.clearDiskCache()

const styles = StyleSheet.create({
imageContainer: {
overflow: 'hidden',
Expand Down