diff --git a/Libraries/Image/RCTImageLoader.h b/Libraries/Image/RCTImageLoader.h index 3f7e7385203afd..5326845ee69ac4 100644 --- a/Libraries/Image/RCTImageLoader.h +++ b/Libraries/Image/RCTImageLoader.h @@ -38,6 +38,18 @@ typedef dispatch_block_t RCTImageLoaderCancellationBlock; @end +/** + * If available, RCTImageRedirectProtocol is invoked before loading an asset. + * Implementation should return either a new URL or nil when redirection is + * not needed. + */ + +@protocol RCTImageRedirectProtocol + +- (NSURL *)redirectAssetsURL:(NSURL *)URL; + +@end + @interface UIImage (React) @property (nonatomic, copy) CAKeyframeAnimation *reactKeyframeAnimation; @@ -71,6 +83,9 @@ typedef dispatch_block_t RCTImageLoaderCancellationBlock; */ @property (nonatomic, assign) NSUInteger maxConcurrentDecodingBytes; +- (instancetype)init; +- (instancetype)initWithRedirectDelegate:(id)redirectDelegate NS_DESIGNATED_INITIALIZER; + /** * Loads the specified image at the highest available resolution. * Can be called from any thread, will call back on an unspecified thread. diff --git a/Libraries/Image/RCTImageLoader.m b/Libraries/Image/RCTImageLoader.m index 7e57f481b2ecd9..8b5b96c7bc70d3 100644 --- a/Libraries/Image/RCTImageLoader.m +++ b/Libraries/Image/RCTImageLoader.m @@ -48,12 +48,26 @@ @implementation RCTImageLoader NSMutableArray *_pendingDecodes; NSInteger _scheduledDecodes; NSUInteger _activeBytes; + __weak id _redirectDelegate; } @synthesize bridge = _bridge; RCT_EXPORT_MODULE() +- (instancetype)init +{ + return [self initWithRedirectDelegate:nil]; +} + +- (instancetype)initWithRedirectDelegate:(id)redirectDelegate +{ + if (self = [super init]) { + _redirectDelegate = redirectDelegate; + } + return self; +} + - (void)setUp { // Set defaults @@ -316,6 +330,9 @@ - (RCTImageLoaderCancellationBlock)_loadImageOrDataWithURLRequest:(NSURLRequest if (request.URL.fileURL && request.URL.pathExtension.length == 0) { mutableRequest.URL = [request.URL URLByAppendingPathExtension:@"png"]; } + if (_redirectDelegate != nil) { + mutableRequest.URL = [_redirectDelegate redirectAssetsURL:mutableRequest.URL]; + } request = mutableRequest; }