Skip to content

Commit 058eeb4

Browse files
p-sunfacebook-github-bot
authored andcommitted
Prefetch images using a lower download priority
Reviewed By: fkgozali Differential Revision: D21881729 fbshipit-source-id: 071a41aef2458df3d9a93a4ab0174af73e85b9fc
1 parent 21d0eb7 commit 058eeb4

8 files changed

+99
-28
lines changed

Libraries/Image/RCTImageLoader.mm

+57-10
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ - (float)handlerPriority
146146
{
147147
return 2;
148148
}
149+
#pragma mark - RCTImageLoaderProtocol 1/3
149150

150151
- (id<RCTImageCache>)imageCache
151152
{
@@ -228,6 +229,8 @@ - (void)setImageCache:(id<RCTImageCache>)cache
228229
return nil;
229230
}
230231

232+
# pragma mark - Private Image Decoding & Resizing
233+
231234
- (id<RCTImageDataDecoder>)imageDataDecoderForData:(NSData *)data
232235
{
233236
if (!_maxConcurrentLoadingTasks) {
@@ -309,24 +312,56 @@ - (void)setImageCache:(id<RCTImageCache>)cache
309312
return image;
310313
}
311314

312-
- (RCTImageLoaderCancellationBlock) loadImageWithURLRequest:(NSURLRequest *)imageURLRequest
313-
callback:(RCTImageLoaderCompletionBlock)callback
315+
#pragma mark - RCTImageLoaderProtocol 2/3
316+
317+
- (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest
318+
callback:(RCTImageLoaderCompletionBlock)callback
319+
{
320+
return [self loadImageWithURLRequest:imageURLRequest
321+
priority:RCTImageLoaderPriorityImmediate
322+
callback:callback];
323+
}
324+
325+
- (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest
326+
priority:(RCTImageLoaderPriority)priority
327+
callback:(RCTImageLoaderCompletionBlock)callback {
328+
return [self loadImageWithURLRequest:imageURLRequest
329+
size:CGSizeZero
330+
scale:1
331+
clipped:YES
332+
resizeMode:RCTResizeModeStretch
333+
priority:priority
334+
progressBlock:nil
335+
partialLoadBlock:nil
336+
completionBlock:callback];
337+
}
338+
339+
- (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest
340+
size:(CGSize)size
341+
scale:(CGFloat)scale
342+
clipped:(BOOL)clipped
343+
resizeMode:(RCTResizeMode)resizeMode
344+
progressBlock:(RCTImageLoaderProgressBlock)progressBlock
345+
partialLoadBlock:(RCTImageLoaderPartialLoadBlock)partialLoadBlock
346+
completionBlock:(RCTImageLoaderCompletionBlock)completionBlock
314347
{
315-
return [self loadImageWithURLRequest:imageURLRequest
316-
size:CGSizeZero
317-
scale:1
318-
clipped:YES
319-
resizeMode:RCTResizeModeStretch
320-
progressBlock:nil
321-
partialLoadBlock:nil
322-
completionBlock:callback];
348+
return [self loadImageWithURLRequest:imageURLRequest
349+
size:size
350+
scale:scale
351+
clipped:clipped
352+
resizeMode:resizeMode
353+
priority:RCTImageLoaderPriorityImmediate
354+
progressBlock:progressBlock
355+
partialLoadBlock:partialLoadBlock
356+
completionBlock:completionBlock];
323357
}
324358

325359
- (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest
326360
size:(CGSize)size
327361
scale:(CGFloat)scale
328362
clipped:(BOOL)clipped
329363
resizeMode:(RCTResizeMode)resizeMode
364+
priority:(RCTImageLoaderPriority)priority
330365
progressBlock:(RCTImageLoaderProgressBlock)progressBlock
331366
partialLoadBlock:(RCTImageLoaderPartialLoadBlock)partialLoadBlock
332367
completionBlock:(RCTImageLoaderCompletionBlock)completionBlock
@@ -336,6 +371,7 @@ - (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)image
336371
scale:scale
337372
clipped:clipped
338373
resizeMode:resizeMode
374+
priority:priority
339375
attribution:{}
340376
progressBlock:progressBlock
341377
partialLoadBlock:partialLoadBlock
@@ -345,6 +381,8 @@ - (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)image
345381
};
346382
}
347383

384+
#pragma mark - Private Downloader Methods
385+
348386
- (void)dequeueTasks
349387
{
350388
dispatch_async(_URLRequestQueue, ^{
@@ -415,6 +453,7 @@ - (RCTImageURLLoaderRequest *)_loadImageOrDataWithURLRequest:(NSURLRequest *)req
415453
size:(CGSize)size
416454
scale:(CGFloat)scale
417455
resizeMode:(RCTResizeMode)resizeMode
456+
priority:(RCTImageLoaderPriority)priority
418457
attribution:(const ImageURLLoaderAttribution &)attribution
419458
progressBlock:(RCTImageLoaderProgressBlock)progressHandler
420459
partialLoadBlock:(RCTImageLoaderPartialLoadBlock)partialLoadHandler
@@ -481,6 +520,7 @@ - (RCTImageURLLoaderRequest *)_loadImageOrDataWithURLRequest:(NSURLRequest *)req
481520
scale:scale
482521
resizeMode:resizeMode
483522
requestId:requestId
523+
priority:priority
484524
attribution:attributionCopy
485525
progressHandler:progressHandler
486526
partialLoadHandler:partialLoadHandler
@@ -520,6 +560,7 @@ - (RCTImageURLLoaderRequest *)_loadImageOrDataWithURLRequest:(NSURLRequest *)req
520560
scale:scale
521561
resizeMode:resizeMode
522562
requestId:requestId
563+
priority:priority
523564
attribution:attributionCopy
524565
progressHandler:progressHandler
525566
partialLoadHandler:partialLoadHandler
@@ -701,6 +742,7 @@ - (RCTImageURLLoaderRequest *)loadImageWithURLRequest:(NSURLRequest *)imageURLRe
701742
scale:(CGFloat)scale
702743
clipped:(BOOL)clipped
703744
resizeMode:(RCTResizeMode)resizeMode
745+
priority:(RCTImageLoaderPriority)priority
704746
attribution:(const ImageURLLoaderAttribution &)attribution
705747
progressBlock:(RCTImageLoaderProgressBlock)progressBlock
706748
partialLoadBlock:(RCTImageLoaderPartialLoadBlock)partialLoadBlock
@@ -768,6 +810,7 @@ - (RCTImageURLLoaderRequest *)loadImageWithURLRequest:(NSURLRequest *)imageURLRe
768810
size:size
769811
scale:scale
770812
resizeMode:resizeMode
813+
priority:priority
771814
attribution:attribution
772815
progressBlock:progressBlock
773816
partialLoadBlock:partialLoadBlock
@@ -825,6 +868,8 @@ - (void)trackURLImageDidDestroy:(RCTImageURLLoaderRequest *)loaderRequest
825868
}
826869
}
827870

871+
#pragma mark - RCTImageLoaderProtocol 3/3
872+
828873
- (RCTImageLoaderCancellationBlock)decodeImageData:(NSData *)data
829874
size:(CGSize)size
830875
scale:(CGFloat)scale
@@ -976,6 +1021,7 @@ - (RCTImageLoaderCancellationBlock)getImageSizeForURLRequest:(NSURLRequest *)ima
9761021
size:CGSizeZero
9771022
scale:1
9781023
resizeMode:RCTResizeModeStretch
1024+
priority: RCTImageLoaderPriorityImmediate
9791025
attribution:{}
9801026
progressBlock:NULL
9811027
partialLoadBlock:NULL
@@ -1134,6 +1180,7 @@ - (void)cancelRequest:(id)requestToken
11341180
{
11351181
NSURLRequest *request = [RCTConvert NSURLRequest:uri];
11361182
[self loadImageWithURLRequest:request
1183+
priority:RCTImageLoaderPriorityPrefetch
11371184
callback:^(NSError *error, UIImage *image) {
11381185
if (error) {
11391186
reject(@"E_PREFETCH_FAILURE", nil, error);

Libraries/Image/RCTImageLoaderProtocol.h

+25-8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@
2626

2727
@end
2828

29+
/**
30+
* Image Downloading priority.
31+
* Use PriorityImmediate to download images at the highest priority.
32+
* Use PriorityPrefetch to prefetch images at a lower priority.
33+
* The priority logic is up to each @RCTImageLoaderProtocol implementation
34+
*/
35+
typedef NS_ENUM(NSUInteger, RCTImageLoaderPriority) {
36+
RCTImageLoaderPriorityImmediate,
37+
RCTImageLoaderPriorityPrefetch
38+
};
39+
2940
@protocol RCTImageLoaderProtocol<RCTURLRequestHandler>
3041

3142
/**
@@ -59,16 +70,22 @@
5970
*/
6071
- (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest
6172
callback:(RCTImageLoaderCompletionBlock)callback;
62-
6373
/**
64-
* As above, but includes target `size`, `scale` and `resizeMode`, which are used to
65-
* select the optimal dimensions for the loaded image. The `clipped` option
66-
* controls whether the image will be clipped to fit the specified size exactly,
67-
* or if the original aspect ratio should be retained.
68-
* `partialLoadBlock` is meant for custom image loaders that do not ship with the core RN library.
69-
* It is meant to be called repeatedly while loading the image as higher quality versions are decoded,
70-
* for instance with progressive JPEGs.
74+
* As above, but includes download `priority`.
7175
*/
76+
- (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest
77+
priority:(RCTImageLoaderPriority)priority
78+
callback:(RCTImageLoaderCompletionBlock)callback;
79+
80+
/**
81+
* As above, but includes target `size`, `scale` and `resizeMode`, which are used to
82+
* select the optimal dimensions for the loaded image. The `clipped` option
83+
* controls whether the image will be clipped to fit the specified size exactly,
84+
* or if the original aspect ratio should be retained.
85+
* `partialLoadBlock` is meant for custom image loaders that do not ship with the core RN library.
86+
* It is meant to be called repeatedly while loading the image as higher quality versions are decoded,
87+
* for instance with progressive JPEGs.
88+
*/
7289
- (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest
7390
size:(CGSize)size
7491
scale:(CGFloat)scale

Libraries/Image/RCTImageLoaderWithAttributionProtocol.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ RCT_EXTERN void RCTEnableImageLoadingPerfInstrumentation(BOOL enabled);
2828
scale:(CGFloat)scale
2929
clipped:(BOOL)clipped
3030
resizeMode:(RCTResizeMode)resizeMode
31+
priority: (RCTImageLoaderPriority)priority
3132
attribution:(const facebook::react::ImageURLLoaderAttribution &)attribution
3233
progressBlock:(RCTImageLoaderProgressBlock)progressBlock
3334
partialLoadBlock:(RCTImageLoaderPartialLoadBlock)partialLoadBlock

Libraries/Image/RCTImageURLLoaderWithAttribution.h

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#import <React/RCTImageURLLoader.h>
9+
#import <React/RCTImageLoaderProtocol.h>
910

1011
// TODO (T61325135): Remove C++ checks
1112
#ifdef __cplusplus
@@ -50,6 +51,7 @@ struct ImageURLLoaderAttribution {
5051
scale:(CGFloat)scale
5152
resizeMode:(RCTResizeMode)resizeMode
5253
requestId:(NSString *)requestId
54+
priority: (RCTImageLoaderPriority)priority
5355
attribution:(const facebook::react::ImageURLLoaderAttribution &)attribution
5456
progressHandler:(RCTImageLoaderProgressBlock)progressHandler
5557
partialLoadHandler:(RCTImageLoaderPartialLoadBlock)partialLoadHandler

Libraries/Image/RCTImageView.mm

+1
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ - (void)reloadImage
345345
scale:imageScale
346346
clipped:NO
347347
resizeMode:_resizeMode
348+
priority:RCTImageLoaderPriorityImmediate
348349
attribution:{
349350
.nativeViewTag = [self.reactTag intValue],
350351
.surfaceId = [self.rootTag intValue],

Libraries/Image/RCTImageViewManager.mm

+11-10
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,17 @@ - (UIView *)view
8989
reject(@"E_INVALID_URI", @"Cannot prefetch an image for an empty URI", nil);
9090
return;
9191
}
92-
93-
[[self.bridge moduleForName:@"ImageLoader" lazilyLoadIfNecessary:YES]
94-
loadImageWithURLRequest:request
95-
callback:^(NSError *error, UIImage *image) {
96-
if (error) {
97-
reject(@"E_PREFETCH_FAILURE", nil, error);
98-
return;
99-
}
100-
resolve(@YES);
101-
}];
92+
id<RCTImageLoaderProtocol> imageLoader = (id<RCTImageLoaderProtocol>)[self.bridge
93+
moduleForName:@"ImageLoader" lazilyLoadIfNecessary:YES];
94+
[imageLoader loadImageWithURLRequest:request
95+
priority:RCTImageLoaderPriorityPrefetch
96+
callback:^(NSError *error, UIImage *image) {
97+
if (error) {
98+
reject(@"E_PREFETCH_FAILURE", nil, error);
99+
return;
100+
}
101+
resolve(@YES);
102+
}];
102103
}
103104

104105
RCT_EXPORT_METHOD(queryCache:(NSArray *)requests

ReactCommon/fabric/imagemanager/platform/ios/RCTImageManager.mm

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ - (ImageRequest)requestImage:(ImageSource)imageSource surfaceId:(SurfaceId)surfa
9090
scale:imageSource.scale
9191
clipped:YES
9292
resizeMode:RCTResizeModeStretch
93+
priority:RCTImageLoaderPriorityImmediate
9394
attribution:{
9495
.surfaceId = surfaceId,
9596
}

ReactCommon/fabric/imagemanager/platform/ios/RCTSyncImageManager.mm

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ - (ImageRequest)requestImage:(ImageSource)imageSource surfaceId:(SurfaceId)surfa
7878
scale:imageSource.scale
7979
clipped:YES
8080
resizeMode:RCTResizeModeStretch
81+
priority:RCTImageLoaderPriorityImmediate
8182
attribution:{
8283
.surfaceId = surfaceId,
8384
}

0 commit comments

Comments
 (0)