15
15
#import < React/RCTDefines.h>
16
16
#import < React/RCTImageCache.h>
17
17
#import < React/RCTImageLoader.h>
18
+ #import < React/RCTImageLoaderWithAttributionProtocol.h>
18
19
#import < React/RCTImageUtils.h>
19
20
#import < React/RCTLog.h>
20
21
#import < React/RCTNetworking.h>
21
22
#import < React/RCTUtils.h>
22
23
23
24
#import " RCTImagePlugins.h"
24
25
26
+ using namespace facebook ::react;
27
+
25
28
static NSInteger RCTImageBytesForImage (UIImage *image)
26
29
{
27
30
NSInteger singleImageBytes = image.size .width * image.size .height * image.scale * image.scale * 4 ;
28
31
return image.images ? image.images .count * singleImageBytes : singleImageBytes;
29
32
}
30
33
31
- @interface RCTImageLoader () <NativeImageLoaderIOSSpec>
34
+ @interface RCTImageLoader () <NativeImageLoaderIOSSpec, RCTImageLoaderWithAttributionProtocol >
32
35
33
36
@end
34
37
@@ -288,11 +291,32 @@ - (RCTImageLoaderCancellationBlock) loadImageWithURLRequest:(NSURLRequest *)imag
288
291
scale: 1
289
292
clipped: YES
290
293
resizeMode: RCTResizeModeStretch
294
+ attribution: {}
291
295
progressBlock: nil
292
296
partialLoadBlock: nil
293
297
completionBlock: callback];
294
298
}
295
299
300
+ - (RCTImageLoaderCancellationBlock)loadImageWithURLRequest : (NSURLRequest *)imageURLRequest
301
+ size : (CGSize )size
302
+ scale : (CGFloat )scale
303
+ clipped : (BOOL )clipped
304
+ resizeMode : (RCTResizeMode)resizeMode
305
+ progressBlock : (RCTImageLoaderProgressBlock)progressBlock
306
+ partialLoadBlock : (RCTImageLoaderPartialLoadBlock)partialLoadBlock
307
+ completionBlock : (RCTImageLoaderCompletionBlock)completionBlock
308
+ {
309
+ return [self loadImageWithURLRequest: imageURLRequest
310
+ size: size
311
+ scale: scale
312
+ clipped: clipped
313
+ resizeMode: resizeMode
314
+ attribution: {}
315
+ progressBlock: progressBlock
316
+ partialLoadBlock: partialLoadBlock
317
+ completionBlock: completionBlock];
318
+ }
319
+
296
320
- (void )dequeueTasks
297
321
{
298
322
dispatch_async (_URLRequestQueue, ^{
@@ -363,6 +387,7 @@ - (RCTImageLoaderCancellationBlock)_loadImageOrDataWithURLRequest:(NSURLRequest
363
387
size : (CGSize )size
364
388
scale : (CGFloat )scale
365
389
resizeMode : (RCTResizeMode)resizeMode
390
+ attribution : (const ImageURLLoaderAttribution &)attribution
366
391
progressBlock : (RCTImageLoaderProgressBlock)progressHandler
367
392
partialLoadBlock : (RCTImageLoaderPartialLoadBlock)partialLoadHandler
368
393
completionBlock : (void (^)(NSError *error, id imageOrData, BOOL cacheResult, NSURLResponse *response))completionBlock
@@ -383,6 +408,9 @@ - (RCTImageLoaderCancellationBlock)_loadImageOrDataWithURLRequest:(NSURLRequest
383
408
request = mutableRequest;
384
409
}
385
410
411
+ // Create a copy here so the value is retained when accessed in the blocks below.
412
+ ImageURLLoaderAttribution attributionCopy (attribution);
413
+
386
414
// Find suitable image URL loader
387
415
id <RCTImageURLLoader> loadHandler = [self imageURLLoaderForURL: request.URL];
388
416
BOOL requiresScheduling = [loadHandler respondsToSelector: @selector (requiresScheduling )] ?
@@ -417,13 +445,25 @@ - (RCTImageLoaderCancellationBlock)_loadImageOrDataWithURLRequest:(NSURLRequest
417
445
// If the loader doesn't require scheduling we call it directly on
418
446
// the main queue.
419
447
if (loadHandler && !requiresScheduling) {
448
+ if ([loadHandler conformsToProtocol: @protocol (RCTImageURLLoaderWithAttribution)]) {
449
+ return [(id <RCTImageURLLoaderWithAttribution>)loadHandler loadImageForURL: request.URL
450
+ size: size
451
+ scale: scale
452
+ resizeMode: resizeMode
453
+ attribution: attributionCopy
454
+ progressHandler: progressHandler
455
+ partialLoadHandler: partialLoadHandler
456
+ completionHandler: ^(NSError *error, UIImage *image) {
457
+ completionHandler (error, image, nil );
458
+ }];
459
+ }
420
460
return [loadHandler loadImageForURL: request.URL
421
461
size: size
422
462
scale: scale
423
463
resizeMode: resizeMode
424
464
progressHandler: progressHandler
425
465
partialLoadHandler: partialLoadHandler
426
- completionHandler: ^(NSError *error, UIImage *image){
466
+ completionHandler: ^(NSError *error, UIImage *image) {
427
467
completionHandler (error, image, nil );
428
468
}];
429
469
}
@@ -441,15 +481,29 @@ - (RCTImageLoaderCancellationBlock)_loadImageOrDataWithURLRequest:(NSURLRequest
441
481
}
442
482
443
483
if (loadHandler) {
444
- dispatch_block_t cancelLoadLocal = [loadHandler loadImageForURL: request.URL
445
- size: size
446
- scale: scale
447
- resizeMode: resizeMode
448
- progressHandler: progressHandler
449
- partialLoadHandler: partialLoadHandler
450
- completionHandler: ^(NSError *error, UIImage *image) {
451
- completionHandler (error, image, nil );
452
- }];
484
+ dispatch_block_t cancelLoadLocal;
485
+ if ([loadHandler conformsToProtocol: @protocol (RCTImageURLLoaderWithAttribution)]) {
486
+ cancelLoadLocal = [(id <RCTImageURLLoaderWithAttribution>)loadHandler loadImageForURL: request.URL
487
+ size: size
488
+ scale: scale
489
+ resizeMode: resizeMode
490
+ attribution: attributionCopy
491
+ progressHandler: progressHandler
492
+ partialLoadHandler: partialLoadHandler
493
+ completionHandler: ^(NSError *error, UIImage *image) {
494
+ completionHandler (error, image, nil );
495
+ }];
496
+ } else {
497
+ cancelLoadLocal = [loadHandler loadImageForURL: request.URL
498
+ size: size
499
+ scale: scale
500
+ resizeMode: resizeMode
501
+ progressHandler: progressHandler
502
+ partialLoadHandler: partialLoadHandler
503
+ completionHandler: ^(NSError *error, UIImage *image) {
504
+ completionHandler (error, image, nil );
505
+ }];
506
+ }
453
507
[cancelLoadLock lock ];
454
508
cancelLoad = cancelLoadLocal;
455
509
[cancelLoadLock unlock ];
@@ -607,6 +661,7 @@ - (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)image
607
661
scale : (CGFloat )scale
608
662
clipped : (BOOL )clipped
609
663
resizeMode : (RCTResizeMode)resizeMode
664
+ attribution : (const ImageURLLoaderAttribution &)attribution
610
665
progressBlock : (RCTImageLoaderProgressBlock)progressBlock
611
666
partialLoadBlock : (RCTImageLoaderPartialLoadBlock)partialLoadBlock
612
667
completionBlock : (RCTImageLoaderCompletionBlock)completionBlock
@@ -673,6 +728,7 @@ - (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)image
673
728
size: size
674
729
scale: scale
675
730
resizeMode: resizeMode
731
+ attribution: attribution
676
732
progressBlock: progressBlock
677
733
partialLoadBlock: partialLoadBlock
678
734
completionBlock: completionHandler];
@@ -830,6 +886,7 @@ - (RCTImageLoaderCancellationBlock)getImageSizeForURLRequest:(NSURLRequest *)ima
830
886
size: CGSizeZero
831
887
scale: 1
832
888
resizeMode: RCTResizeModeStretch
889
+ attribution: {}
833
890
progressBlock: NULL
834
891
partialLoadBlock: NULL
835
892
completionBlock: completion];
0 commit comments