RCTLocalAssetImageLoader.mm 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import <React/RCTLocalAssetImageLoader.h>
  8. #import <atomic>
  9. #import <memory>
  10. #import <React/RCTUtils.h>
  11. #import <ReactCommon/RCTTurboModule.h>
  12. #import "RCTImagePlugins.h"
  13. @interface RCTLocalAssetImageLoader() <RCTTurboModule>
  14. @end
  15. @implementation RCTLocalAssetImageLoader
  16. RCT_EXPORT_MODULE()
  17. - (BOOL)canLoadImageURL:(NSURL *)requestURL
  18. {
  19. return RCTIsLocalAssetURL(requestURL);
  20. }
  21. - (BOOL)requiresScheduling
  22. {
  23. // Don't schedule this loader on the URL queue so we can load the
  24. // local assets synchronously to avoid flickers.
  25. return NO;
  26. }
  27. - (BOOL)shouldCacheLoadedImages
  28. {
  29. // UIImage imageNamed handles the caching automatically so we don't want
  30. // to add it to the image cache.
  31. return NO;
  32. }
  33. - (nullable RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL
  34. size:(CGSize)size
  35. scale:(CGFloat)scale
  36. resizeMode:(RCTResizeMode)resizeMode
  37. progressHandler:(RCTImageLoaderProgressBlock)progressHandler
  38. partialLoadHandler:(RCTImageLoaderPartialLoadBlock)partialLoadHandler
  39. completionHandler:(RCTImageLoaderCompletionBlock)completionHandler
  40. {
  41. UIImage *image = RCTImageFromLocalAssetURL(imageURL);
  42. if (image) {
  43. if (progressHandler) {
  44. progressHandler(1, 1);
  45. }
  46. completionHandler(nil, image);
  47. } else {
  48. NSString *message = [NSString stringWithFormat:@"Could not find image %@", imageURL];
  49. RCTLogWarn(@"%@", message);
  50. completionHandler(RCTErrorWithMessage(message), nil);
  51. }
  52. return nil;
  53. }
  54. @end
  55. Class RCTLocalAssetImageLoaderCls(void) {
  56. return RCTLocalAssetImageLoader.class;
  57. }