Add custom cache setter

Reviewed By: donyu

Differential Revision: D3622912

fbshipit-source-id: 675b3135212c73808cb18702c2cbf5684816df87
This commit is contained in:
Maria Mateescu 2016-08-01 22:00:56 -07:00 committed by Facebook Github Bot
parent b44d617999
commit 2a823bbc5c
3 changed files with 48 additions and 34 deletions

View File

@ -11,5 +11,5 @@
#import "RCTImageLoader.h"
@interface RCTImageCache : NSObject <RCTImageCacheDelegate>
@interface RCTImageCache : NSObject <RCTImageCache>
@end

View File

@ -17,6 +17,26 @@ typedef void (^RCTImageLoaderProgressBlock)(int64_t progress, int64_t total);
typedef void (^RCTImageLoaderCompletionBlock)(NSError *error, UIImage *image);
typedef dispatch_block_t RCTImageLoaderCancellationBlock;
/**
* Provides an interface to use for providing a image caching strategy.
*/
@protocol RCTImageCache <NSObject>
- (UIImage *)imageForUrl:(NSString *)url
size:(CGSize)size
scale:(CGFloat)scale
resizeMode:(RCTResizeMode)resizeMode
responseDate:(NSString *)responseDate;
- (void)addImageToCache:(UIImage *)image
URL:(NSString *)url
size:(CGSize)size
scale:(CGFloat)scale
resizeMode:(RCTResizeMode)resizeMode
responseDate:(NSString *)responseDate;
@end
@interface UIImage (React)
@property (nonatomic, copy) CAKeyframeAnimation *reactKeyframeAnimation;
@ -92,6 +112,13 @@ typedef dispatch_block_t RCTImageLoaderCancellationBlock;
- (RCTImageLoaderCancellationBlock)getImageSizeForURLRequest:(NSURLRequest *)imageURLRequest
block:(void(^)(NSError *error, CGSize size))completionBlock;
/**
* Allows developers to set their own caching implementation for
* decoded images as long as it conforms to the RCTImageCacheDelegate
* protocol. This method should be called in bridgeDidInitializeModule.
*/
- (void)setImageCache:(id<RCTImageCache>)cache;
@end
@interface RCTBridge (RCTImageLoader)
@ -198,24 +225,3 @@ typedef dispatch_block_t RCTImageLoaderCancellationBlock;
- (float)decoderPriority;
@end
/**
* Provides an interface to use for providing a image caching strategy.
*/
@protocol RCTImageCacheDelegate <NSObject>
- (UIImage *)imageForUrl:(NSString *)url
size:(CGSize)size
scale:(CGFloat)scale
resizeMode:(RCTResizeMode)resizeMode
responseDate:(NSString *)responseDate;
- (void)addImageToCache:(UIImage *)image
URL:(NSString *)url
size:(CGSize)size
scale:(CGFloat)scale
resizeMode:(RCTResizeMode)resizeMode
responseDate:(NSString *)responseDate;
@end

View File

@ -43,7 +43,7 @@
NSArray<id<RCTImageDataDecoder>> *_decoders;
NSOperationQueue *_imageDecodeQueue;
dispatch_queue_t _URLRequestQueue;
id<RCTImageCacheDelegate> _imageCache;
id<RCTImageCache> _imageCache;
NSMutableArray *_pendingTasks;
NSInteger _activeTasks;
NSMutableArray *_pendingDecodes;
@ -65,20 +65,28 @@ RCT_EXPORT_MODULE()
_URLRequestQueue = dispatch_queue_create("com.facebook.react.ImageLoaderURLRequestQueue", DISPATCH_QUEUE_SERIAL);
}
- (id<RCTImageCacheDelegate>)getImageCache
{
if (!_imageCache) {
//set up with default cache
_imageCache = [RCTImageCache new];
}
return _imageCache;
}
- (float)handlerPriority
{
return 1;
}
- (id<RCTImageCache>)imageCache
{
if (!_imageCache) {
//set up with default cache
_imageCache = [RCTImageCache new];
}
return _imageCache;
}
- (void)setImageCache:(id<RCTImageCache>)cache
{
if (_imageCache) {
RCTLogWarn(@"RCTImageCache was already set and has now been overriden.");
}
_imageCache = cache;
}
- (id<RCTImageURLLoader>)imageURLLoaderForURL:(NSURL *)URL
{
if (!_maxConcurrentLoadingTasks) {
@ -500,7 +508,7 @@ static UIImage *RCTResizeImageIfNeeded(UIImage *image,
// Check decoded image cache
if (cacheResult) {
UIImage *image = [[strongSelf getImageCache] imageForUrl:imageURLRequest.URL.absoluteString
UIImage *image = [[strongSelf imageCache] imageForUrl:imageURLRequest.URL.absoluteString
size:size
scale:scale
resizeMode:resizeMode
@ -514,7 +522,7 @@ static UIImage *RCTResizeImageIfNeeded(UIImage *image,
// Store decoded image in cache
RCTImageLoaderCompletionBlock cacheResultHandler = ^(NSError *error_, UIImage *image) {
if (image) {
[[strongSelf getImageCache] addImageToCache:image
[[strongSelf imageCache] addImageToCache:image
URL:imageURLRequest.URL.absoluteString
size:size
scale:scale