@@ -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, NSString *cacheControl ))completionBlock
324
+ completionBlock : (void (^)(NSError *error, id imageOrData, BOOL cacheResult, NSString *fetchDate))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
-
350
347
__block atomic_bool cancelled = ATOMIC_VAR_INIT (NO );
351
348
// TODO: Protect this variable shared between threads.
352
349
__block dispatch_block_t cancelLoad = nil ;
353
- void (^completionHandler)(NSError *, id , NSString *, NSString * ) = ^(NSError *error, id imageOrData, NSString *fetchDate, NSString *cacheControl ) {
350
+ void (^completionHandler)(NSError *, id , NSString *) = ^(NSError *error, id imageOrData, NSString *fetchDate) {
354
351
cancelLoad = nil ;
355
352
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, cacheControl );
363
+ completionBlock (error, imageOrData, cacheResult, fetchDate);
364
364
}
365
365
});
366
366
} else if (!atomic_load (&cancelled)) {
367
- completionBlock (error, imageOrData, cacheResult, fetchDate, cacheControl );
367
+ completionBlock (error, imageOrData, cacheResult, fetchDate);
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 , nil );
381
+ completionHandler (error, image, nil );
382
382
}];
383
383
}
384
384
@@ -402,25 +402,13 @@ - (RCTImageLoaderCancellationBlock)_loadImageOrDataWithURLRequest:(NSURLRequest
402
402
progressHandler: progressHandler
403
403
partialLoadHandler: partialLoadHandler
404
404
completionHandler: ^(NSError *error, UIImage *image) {
405
- completionHandler (error, image, nil , nil );
405
+ completionHandler (error, image, nil );
406
406
}];
407
407
} else {
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
- }
408
+ // Use networking module to load image
409
+ cancelLoad = [strongSelf _loadURLRequest: request
410
+ progressBlock: progressHandler
411
+ completionBlock: completionHandler];
424
412
}
425
413
});
426
414
@@ -439,7 +427,7 @@ - (RCTImageLoaderCancellationBlock)_loadImageOrDataWithURLRequest:(NSURLRequest
439
427
440
428
- (RCTImageLoaderCancellationBlock)_loadURLRequest : (NSURLRequest *)request
441
429
progressBlock : (RCTImageLoaderProgressBlock)progressHandler
442
- completionBlock : (void (^)(NSError *error, id imageOrData, NSString *fetchDate, NSString *cacheControl ))completionHandler
430
+ completionBlock : (void (^)(NSError *error, id imageOrData, NSString *fetchDate))completionHandler
443
431
{
444
432
// Check if networking module is available
445
433
if (RCT_DEBUG && ![_bridge respondsToSelector: @selector (networking )]) {
@@ -461,36 +449,34 @@ - (RCTImageLoaderCancellationBlock)_loadURLRequest:(NSURLRequest *)request
461
449
RCTURLRequestCompletionBlock processResponse = ^(NSURLResponse *response, NSData *data, NSError *error) {
462
450
// Check for system errors
463
451
if (error) {
464
- completionHandler (error, nil , nil , nil );
452
+ completionHandler (error, nil , nil );
465
453
return ;
466
454
} else if (!response) {
467
- completionHandler (RCTErrorWithMessage (@" Response metadata error" ), nil , nil , nil );
455
+ completionHandler (RCTErrorWithMessage (@" Response metadata error" ), nil , nil );
468
456
return ;
469
457
} else if (!data) {
470
- completionHandler (RCTErrorWithMessage (@" Unknown image download error" ), nil , nil , nil );
458
+ completionHandler (RCTErrorWithMessage (@" Unknown image download error" ), nil , nil );
471
459
return ;
472
460
}
473
461
474
462
// Check for http errors
475
463
NSString *responseDate;
476
- NSString *cacheControl;
477
464
if ([response isKindOfClass: [NSHTTPURLResponse class ]]) {
478
465
NSInteger statusCode = ((NSHTTPURLResponse *)response).statusCode ;
479
466
if (statusCode != 200 ) {
480
467
NSString *errorMessage = [NSString stringWithFormat: @" Failed to load %@ " , response.URL];
481
468
NSDictionary *userInfo = @{NSLocalizedDescriptionKey : errorMessage};
482
469
completionHandler ([[NSError alloc ] initWithDomain: NSURLErrorDomain
483
470
code: statusCode
484
- userInfo: userInfo], nil , nil , nil );
471
+ userInfo: userInfo], nil , nil );
485
472
return ;
486
473
}
487
474
488
475
responseDate = ((NSHTTPURLResponse *)response).allHeaderFields [@" Date" ];
489
- cacheControl = ((NSHTTPURLResponse *)response).allHeaderFields [@" Cache-Control" ];
490
476
}
491
477
492
478
// Call handler
493
- completionHandler (nil , data, responseDate, cacheControl );
479
+ completionHandler (nil , data, responseDate);
494
480
};
495
481
496
482
// Download image
@@ -512,7 +498,7 @@ - (RCTImageLoaderCancellationBlock)_loadURLRequest:(NSURLRequest *)request
512
498
} else {
513
499
someError = RCTErrorWithMessage (@" Unknown image download error" );
514
500
}
515
- completionHandler (someError, nil , nil , nil );
501
+ completionHandler (someError, nil , nil );
516
502
[strongSelf dequeueTasks ];
517
503
return ;
518
504
}
@@ -578,7 +564,7 @@ - (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)image
578
564
};
579
565
580
566
__weak RCTImageLoader *weakSelf = self;
581
- void (^completionHandler)(NSError *, id , BOOL , NSString *, NSString * ) = ^(NSError *error, id imageOrData, BOOL cacheResult, NSString *fetchDate, NSString *cacheControl ) {
567
+ void (^completionHandler)(NSError *, id , BOOL , NSString *) = ^(NSError *error, id imageOrData, BOOL cacheResult, NSString *fetchDate) {
582
568
__typeof (self) strongSelf = weakSelf;
583
569
if (atomic_load (&cancelled) || !strongSelf) {
584
570
return ;
@@ -590,6 +576,20 @@ - (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)image
590
576
return ;
591
577
}
592
578
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,8 +598,7 @@ - (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)image
598
598
size: size
599
599
scale: scale
600
600
resizeMode: resizeMode
601
- responseDate: fetchDate
602
- cacheControl: cacheControl];
601
+ responseDate: fetchDate];
603
602
}
604
603
605
604
cancelLoad = nil ;
@@ -733,7 +732,7 @@ - (RCTImageLoaderCancellationBlock)decodeImageData:(NSData *)data
733
732
- (RCTImageLoaderCancellationBlock)getImageSizeForURLRequest : (NSURLRequest *)imageURLRequest
734
733
block : (void (^)(NSError *error, CGSize size))callback
735
734
{
736
- void (^completion)(NSError *, id , BOOL , NSString *, NSString * ) = ^(NSError *error, id imageOrData, BOOL cacheResult, NSString *fetchDate, NSString *cacheControl ) {
735
+ void (^completion)(NSError *, id , BOOL , NSString *) = ^(NSError *error, id imageOrData, BOOL cacheResult, NSString *fetchDate) {
737
736
CGSize size;
738
737
if ([imageOrData isKindOfClass: [NSData class ]]) {
739
738
NSDictionary *meta = RCTGetImageMetadata (imageOrData);
0 commit comments