Skip to content

Commit

Permalink
Providing an API to load assets from a different directory
Browse files Browse the repository at this point in the history
Differential Revision: D6368586

fbshipit-source-id: 2a79ba45de3d9c95d0b1b296c9e1ae35cbd33c36
  • Loading branch information
Yujie Liu authored and facebook-github-bot committed Dec 8, 2017
1 parent 5ee27ff commit 8f9b291
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Libraries/Image/RCTImageLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,6 +83,9 @@ typedef dispatch_block_t RCTImageLoaderCancellationBlock;
*/
@property (nonatomic, assign) NSUInteger maxConcurrentDecodingBytes;

- (instancetype)init;
- (instancetype)initWithRedirectDelegate:(id<RCTImageRedirectProtocol>)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.
Expand Down
17 changes: 17 additions & 0 deletions Libraries/Image/RCTImageLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,26 @@ @implementation RCTImageLoader
NSMutableArray *_pendingDecodes;
NSInteger _scheduledDecodes;
NSUInteger _activeBytes;
__weak id<RCTImageRedirectProtocol> _redirectDelegate;
}

@synthesize bridge = _bridge;

RCT_EXPORT_MODULE()

- (instancetype)init
{
return [self initWithRedirectDelegate:nil];
}

- (instancetype)initWithRedirectDelegate:(id<RCTImageRedirectProtocol>)redirectDelegate
{
if (self = [super init]) {
_redirectDelegate = redirectDelegate;
}
return self;
}

- (void)setUp
{
// Set defaults
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 8f9b291

Please sign in to comment.