feat: add clear image cache from memory and disk (#425)
This commit is contained in:
parent
4fc024b3bf
commit
818ed0c3f5
|
@ -4,6 +4,7 @@ import android.app.Activity;
|
|||
|
||||
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;
|
||||
|
@ -53,4 +54,33 @@ class FastImageViewModule extends ReactContextBaseJavaModule {
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#import "FFFastImageViewManager.h"
|
||||
#import "FFFastImageView.h"
|
||||
|
||||
#import <SDWebImage/SDImageCache.h>
|
||||
#import <SDWebImage/SDWebImagePrefetcher.h>
|
||||
|
||||
@implementation FFFastImageViewManager
|
||||
|
@ -34,5 +35,17 @@ RCT_EXPORT_METHOD(preload:(nonnull NSArray<FFFastImageSource *> *)sources)
|
|||
[[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
|
||||
|
|
|
@ -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>;
|
||||
}
|
||||
|
|
|
@ -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> &
|
||||
|
@ -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',
|
||||
|
|
Loading…
Reference in New Issue