@@ -321,7 +321,7 @@ - (RCTImageLoaderCancellationBlock)_loadImageOrDataWithURLRequest:(NSURLRequest
321
321
resizeMode : (RCTResizeMode)resizeMode
322
322
progressBlock : (RCTImageLoaderProgressBlock)progressHandler
323
323
partialLoadBlock : (RCTImageLoaderPartialLoadBlock)partialLoadHandler
324
- completionBlock : (void (^)(NSError *error, id imageOrData, BOOL cacheResult, NSString *fetchDate))completionBlock
324
+ completionBlock : (void (^)(NSError *error, id imageOrData, BOOL cacheResult, NSString *fetchDate, NSString *cacheControl ))completionBlock
325
325
{
326
326
{
327
327
NSMutableURLRequest *mutableRequest = [request mutableCopy ];
@@ -344,27 +344,27 @@ - (RCTImageLoaderCancellationBlock)_loadImageOrDataWithURLRequest:(NSURLRequest
344
344
BOOL requiresScheduling = [loadHandler respondsToSelector: @selector (requiresScheduling )] ?
345
345
[loadHandler requiresScheduling ] : YES ;
346
346
347
+ BOOL cacheResult = [loadHandler respondsToSelector: @selector (shouldCacheLoadedImages )] ?
348
+ [loadHandler shouldCacheLoadedImages ] : YES ;
349
+
347
350
__block atomic_bool cancelled = ATOMIC_VAR_INIT (NO );
348
351
// TODO: Protect this variable shared between threads.
349
352
__block dispatch_block_t cancelLoad = nil ;
350
- void (^completionHandler)(NSError *, id , NSString *) = ^(NSError *error, id imageOrData, NSString *fetchDate) {
353
+ void (^completionHandler)(NSError *, id , NSString *, NSString * ) = ^(NSError *error, id imageOrData, NSString *fetchDate, NSString *cacheControl ) {
351
354
cancelLoad = nil ;
352
355
353
- BOOL cacheResult = [loadHandler respondsToSelector: @selector (shouldCacheLoadedImages )] ?
354
- [loadHandler shouldCacheLoadedImages ] : YES ;
355
-
356
356
// If we've received an image, we should try to set it synchronously,
357
357
// if it's data, do decoding on a background thread.
358
358
if (RCTIsMainQueue () && ![imageOrData isKindOfClass: [UIImage class ]]) {
359
359
// Most loaders do not return on the main thread, so caller is probably not
360
360
// expecting it, and may do expensive post-processing in the callback
361
361
dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
362
362
if (!atomic_load (&cancelled)) {
363
- completionBlock (error, imageOrData, cacheResult, fetchDate);
363
+ completionBlock (error, imageOrData, cacheResult, fetchDate, cacheControl );
364
364
}
365
365
});
366
366
} else if (!atomic_load (&cancelled)) {
367
- completionBlock (error, imageOrData, cacheResult, fetchDate);
367
+ completionBlock (error, imageOrData, cacheResult, fetchDate, cacheControl );
368
368
}
369
369
};
370
370
@@ -378,7 +378,7 @@ - (RCTImageLoaderCancellationBlock)_loadImageOrDataWithURLRequest:(NSURLRequest
378
378
progressHandler: progressHandler
379
379
partialLoadHandler: partialLoadHandler
380
380
completionHandler: ^(NSError *error, UIImage *image){
381
- completionHandler (error, image, nil );
381
+ completionHandler (error, image, nil , nil );
382
382
}];
383
383
}
384
384
@@ -402,13 +402,25 @@ - (RCTImageLoaderCancellationBlock)_loadImageOrDataWithURLRequest:(NSURLRequest
402
402
progressHandler: progressHandler
403
403
partialLoadHandler: partialLoadHandler
404
404
completionHandler: ^(NSError *error, UIImage *image) {
405
- completionHandler (error, image, nil );
405
+ completionHandler (error, image, nil , nil );
406
406
}];
407
407
} else {
408
- // Use networking module to load image
409
- cancelLoad = [strongSelf _loadURLRequest: request
410
- progressBlock: progressHandler
411
- completionBlock: completionHandler];
408
+ UIImage *image;
409
+ if (cacheResult) {
410
+ image = [[strongSelf imageCache ] imageForUrl: request.URL.absoluteString
411
+ size: size
412
+ scale: scale
413
+ resizeMode: resizeMode];
414
+ }
415
+
416
+ if (image) {
417
+ completionHandler (nil , image, nil , nil );
418
+ } else {
419
+ // Use networking module to load image
420
+ cancelLoad = [strongSelf _loadURLRequest: request
421
+ progressBlock: progressHandler
422
+ completionBlock: completionHandler];
423
+ }
412
424
}
413
425
});
414
426
@@ -427,7 +439,7 @@ - (RCTImageLoaderCancellationBlock)_loadImageOrDataWithURLRequest:(NSURLRequest
427
439
428
440
- (RCTImageLoaderCancellationBlock)_loadURLRequest : (NSURLRequest *)request
429
441
progressBlock : (RCTImageLoaderProgressBlock)progressHandler
430
- completionBlock : (void (^)(NSError *error, id imageOrData, NSString *fetchDate))completionHandler
442
+ completionBlock : (void (^)(NSError *error, id imageOrData, NSString *fetchDate, NSString *cacheControl ))completionHandler
431
443
{
432
444
// Check if networking module is available
433
445
if (RCT_DEBUG && ![_bridge respondsToSelector: @selector (networking )]) {
@@ -449,34 +461,36 @@ - (RCTImageLoaderCancellationBlock)_loadURLRequest:(NSURLRequest *)request
449
461
RCTURLRequestCompletionBlock processResponse = ^(NSURLResponse *response, NSData *data, NSError *error) {
450
462
// Check for system errors
451
463
if (error) {
452
- completionHandler (error, nil , nil );
464
+ completionHandler (error, nil , nil , nil );
453
465
return ;
454
466
} else if (!response) {
455
- completionHandler (RCTErrorWithMessage (@" Response metadata error" ), nil , nil );
467
+ completionHandler (RCTErrorWithMessage (@" Response metadata error" ), nil , nil , nil );
456
468
return ;
457
469
} else if (!data) {
458
- completionHandler (RCTErrorWithMessage (@" Unknown image download error" ), nil , nil );
470
+ completionHandler (RCTErrorWithMessage (@" Unknown image download error" ), nil , nil , nil );
459
471
return ;
460
472
}
461
473
462
474
// Check for http errors
463
475
NSString *responseDate;
476
+ NSString *cacheControl;
464
477
if ([response isKindOfClass: [NSHTTPURLResponse class ]]) {
465
478
NSInteger statusCode = ((NSHTTPURLResponse *)response).statusCode ;
466
479
if (statusCode != 200 ) {
467
480
NSString *errorMessage = [NSString stringWithFormat: @" Failed to load %@ " , response.URL];
468
481
NSDictionary *userInfo = @{NSLocalizedDescriptionKey : errorMessage};
469
482
completionHandler ([[NSError alloc ] initWithDomain: NSURLErrorDomain
470
483
code: statusCode
471
- userInfo: userInfo], nil , nil );
484
+ userInfo: userInfo], nil , nil , nil );
472
485
return ;
473
486
}
474
487
475
488
responseDate = ((NSHTTPURLResponse *)response).allHeaderFields [@" Date" ];
489
+ cacheControl = ((NSHTTPURLResponse *)response).allHeaderFields [@" Cache-Control" ];
476
490
}
477
491
478
492
// Call handler
479
- completionHandler (nil , data, responseDate);
493
+ completionHandler (nil , data, responseDate, cacheControl );
480
494
};
481
495
482
496
// Download image
@@ -498,7 +512,7 @@ - (RCTImageLoaderCancellationBlock)_loadURLRequest:(NSURLRequest *)request
498
512
} else {
499
513
someError = RCTErrorWithMessage (@" Unknown image download error" );
500
514
}
501
- completionHandler (someError, nil , nil );
515
+ completionHandler (someError, nil , nil , nil );
502
516
[strongSelf dequeueTasks ];
503
517
return ;
504
518
}
@@ -564,7 +578,7 @@ - (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)image
564
578
};
565
579
566
580
__weak RCTImageLoader *weakSelf = self;
567
- void (^completionHandler)(NSError *, id , BOOL , NSString *) = ^(NSError *error, id imageOrData, BOOL cacheResult, NSString *fetchDate) {
581
+ void (^completionHandler)(NSError *, id , BOOL , NSString *, NSString * ) = ^(NSError *error, id imageOrData, BOOL cacheResult, NSString *fetchDate, NSString *cacheControl ) {
568
582
__typeof (self) strongSelf = weakSelf;
569
583
if (atomic_load (&cancelled) || !strongSelf) {
570
584
return ;
@@ -576,20 +590,6 @@ - (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)image
576
590
return ;
577
591
}
578
592
579
- // Check decoded image cache
580
- if (cacheResult) {
581
- UIImage *image = [[strongSelf imageCache ] imageForUrl: imageURLRequest.URL.absoluteString
582
- size: size
583
- scale: scale
584
- resizeMode: resizeMode
585
- responseDate: fetchDate];
586
- if (image) {
587
- cancelLoad = nil ;
588
- completionBlock (nil , image);
589
- return ;
590
- }
591
- }
592
-
593
593
RCTImageLoaderCompletionBlock decodeCompletionHandler = ^(NSError *error_, UIImage *image) {
594
594
if (cacheResult && image) {
595
595
// Store decoded image in cache
@@ -598,7 +598,8 @@ - (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)image
598
598
size: size
599
599
scale: scale
600
600
resizeMode: resizeMode
601
- responseDate: fetchDate];
601
+ responseDate: fetchDate
602
+ cacheControl: cacheControl];
602
603
}
603
604
604
605
cancelLoad = nil ;
@@ -732,7 +733,7 @@ - (RCTImageLoaderCancellationBlock)decodeImageData:(NSData *)data
732
733
- (RCTImageLoaderCancellationBlock)getImageSizeForURLRequest : (NSURLRequest *)imageURLRequest
733
734
block : (void (^)(NSError *error, CGSize size))callback
734
735
{
735
- void (^completion)(NSError *, id , BOOL , NSString *) = ^(NSError *error, id imageOrData, BOOL cacheResult, NSString *fetchDate) {
736
+ void (^completion)(NSError *, id , BOOL , NSString *, NSString * ) = ^(NSError *error, id imageOrData, BOOL cacheResult, NSString *fetchDate, NSString *cacheControl ) {
736
737
CGSize size;
737
738
if ([imageOrData isKindOfClass: [NSData class ]]) {
738
739
NSDictionary *meta = RCTGetImageMetadata (imageOrData);
0 commit comments