Skip to content

Commit 8f9b291

Browse files
Yujie Liufacebook-github-bot
Yujie Liu
authored andcommitted
Providing an API to load assets from a different directory
Differential Revision: D6368586 fbshipit-source-id: 2a79ba45de3d9c95d0b1b296c9e1ae35cbd33c36
1 parent 5ee27ff commit 8f9b291

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Libraries/Image/RCTImageLoader.h

+15
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ typedef dispatch_block_t RCTImageLoaderCancellationBlock;
3838

3939
@end
4040

41+
/**
42+
* If available, RCTImageRedirectProtocol is invoked before loading an asset.
43+
* Implementation should return either a new URL or nil when redirection is
44+
* not needed.
45+
*/
46+
47+
@protocol RCTImageRedirectProtocol
48+
49+
- (NSURL *)redirectAssetsURL:(NSURL *)URL;
50+
51+
@end
52+
4153
@interface UIImage (React)
4254

4355
@property (nonatomic, copy) CAKeyframeAnimation *reactKeyframeAnimation;
@@ -71,6 +83,9 @@ typedef dispatch_block_t RCTImageLoaderCancellationBlock;
7183
*/
7284
@property (nonatomic, assign) NSUInteger maxConcurrentDecodingBytes;
7385

86+
- (instancetype)init;
87+
- (instancetype)initWithRedirectDelegate:(id<RCTImageRedirectProtocol>)redirectDelegate NS_DESIGNATED_INITIALIZER;
88+
7489
/**
7590
* Loads the specified image at the highest available resolution.
7691
* Can be called from any thread, will call back on an unspecified thread.

Libraries/Image/RCTImageLoader.m

+17
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,26 @@ @implementation RCTImageLoader
4848
NSMutableArray *_pendingDecodes;
4949
NSInteger _scheduledDecodes;
5050
NSUInteger _activeBytes;
51+
__weak id<RCTImageRedirectProtocol> _redirectDelegate;
5152
}
5253

5354
@synthesize bridge = _bridge;
5455

5556
RCT_EXPORT_MODULE()
5657

58+
- (instancetype)init
59+
{
60+
return [self initWithRedirectDelegate:nil];
61+
}
62+
63+
- (instancetype)initWithRedirectDelegate:(id<RCTImageRedirectProtocol>)redirectDelegate
64+
{
65+
if (self = [super init]) {
66+
_redirectDelegate = redirectDelegate;
67+
}
68+
return self;
69+
}
70+
5771
- (void)setUp
5872
{
5973
// Set defaults
@@ -316,6 +330,9 @@ - (RCTImageLoaderCancellationBlock)_loadImageOrDataWithURLRequest:(NSURLRequest
316330
if (request.URL.fileURL && request.URL.pathExtension.length == 0) {
317331
mutableRequest.URL = [request.URL URLByAppendingPathExtension:@"png"];
318332
}
333+
if (_redirectDelegate != nil) {
334+
mutableRequest.URL = [_redirectDelegate redirectAssetsURL:mutableRequest.URL];
335+
}
319336
request = mutableRequest;
320337
}
321338

0 commit comments

Comments
 (0)