RCTImageLoaderProtocol.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <UIKit/UIKit.h>
  8. #import <React/RCTBridge.h>
  9. #import <React/RCTResizeMode.h>
  10. #import <React/RCTURLRequestHandler.h>
  11. #import <React/RCTImageDataDecoder.h>
  12. #import <React/RCTImageURLLoader.h>
  13. #import <React/RCTImageCache.h>
  14. /**
  15. * If available, RCTImageRedirectProtocol is invoked before loading an asset.
  16. * Implementation should return either a new URL or nil when redirection is
  17. * not needed.
  18. */
  19. @protocol RCTImageRedirectProtocol
  20. - (NSURL *)redirectAssetsURL:(NSURL *)URL;
  21. @end
  22. /**
  23. * Image Downloading priority.
  24. * Use PriorityImmediate to download images at the highest priority.
  25. * Use PriorityPrefetch to prefetch images at a lower priority.
  26. * The priority logic is up to each @RCTImageLoaderProtocol implementation
  27. */
  28. typedef NS_ENUM(NSUInteger, RCTImageLoaderPriority) {
  29. RCTImageLoaderPriorityImmediate,
  30. RCTImageLoaderPriorityPrefetch
  31. };
  32. @protocol RCTImageLoaderProtocol<RCTURLRequestHandler>
  33. /**
  34. * The maximum number of concurrent image loading tasks. Loading and decoding
  35. * images can consume a lot of memory, so setting this to a higher value may
  36. * cause memory to spike. If you are seeing out-of-memory crashes, try reducing
  37. * this value.
  38. */
  39. @property (nonatomic, assign) NSUInteger maxConcurrentLoadingTasks;
  40. /**
  41. * The maximum number of concurrent image decoding tasks. Decoding large
  42. * images can be especially CPU and memory intensive, so if your are decoding a
  43. * lot of large images in your app, you may wish to adjust this value.
  44. */
  45. @property (nonatomic, assign) NSUInteger maxConcurrentDecodingTasks;
  46. /**
  47. * Decoding large images can use a lot of memory, and potentially cause the app
  48. * to crash. This value allows you to throttle the amount of memory used by the
  49. * decoder independently of the number of concurrent threads. This means you can
  50. * still decode a lot of small images in parallel, without allowing the decoder
  51. * to try to decompress multiple huge images at once. Note that this value is
  52. * only a hint, and not an indicator of the total memory used by the app.
  53. */
  54. @property (nonatomic, assign) NSUInteger maxConcurrentDecodingBytes;
  55. /**
  56. * Loads the specified image at the highest available resolution.
  57. * Can be called from any thread, will call back on an unspecified thread.
  58. */
  59. - (nullable RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest
  60. callback:(RCTImageLoaderCompletionBlock)callback;
  61. /**
  62. * As above, but includes download `priority`.
  63. */
  64. - (nullable RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest
  65. priority:(RCTImageLoaderPriority)priority
  66. callback:(RCTImageLoaderCompletionBlock)callback;
  67. /**
  68. * As above, but includes target `size`, `scale` and `resizeMode`, which are used to
  69. * select the optimal dimensions for the loaded image. The `clipped` option
  70. * controls whether the image will be clipped to fit the specified size exactly,
  71. * or if the original aspect ratio should be retained.
  72. * `partialLoadBlock` is meant for custom image loaders that do not ship with the core RN library.
  73. * It is meant to be called repeatedly while loading the image as higher quality versions are decoded,
  74. * for instance with progressive JPEGs.
  75. */
  76. - (nullable RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest
  77. size:(CGSize)size
  78. scale:(CGFloat)scale
  79. clipped:(BOOL)clipped
  80. resizeMode:(RCTResizeMode)resizeMode
  81. progressBlock:(RCTImageLoaderProgressBlock)progressBlock
  82. partialLoadBlock:(RCTImageLoaderPartialLoadBlock)partialLoadBlock
  83. completionBlock:(RCTImageLoaderCompletionBlock)completionBlock;
  84. /**
  85. * Finds an appropriate image decoder and passes the target `size`, `scale` and
  86. * `resizeMode` for optimal image decoding. The `clipped` option controls
  87. * whether the image will be clipped to fit the specified size exactly, or
  88. * if the original aspect ratio should be retained. Can be called from any
  89. * thread, will call callback on an unspecified thread.
  90. */
  91. - (RCTImageLoaderCancellationBlock)decodeImageData:(NSData *)imageData
  92. size:(CGSize)size
  93. scale:(CGFloat)scale
  94. clipped:(BOOL)clipped
  95. resizeMode:(RCTResizeMode)resizeMode
  96. completionBlock:(RCTImageLoaderCompletionBlock)completionBlock;
  97. /**
  98. * Get image size, in pixels. This method will do the least work possible to get
  99. * the information, and won't decode the image if it doesn't have to.
  100. */
  101. - (RCTImageLoaderCancellationBlock)getImageSizeForURLRequest:(NSURLRequest *)imageURLRequest
  102. block:(void(^)(NSError *error, CGSize size))completionBlock;
  103. /**
  104. * Determines whether given image URLs are cached locally. The `requests` array is expected
  105. * to contain objects convertible to NSURLRequest. The return value maps URLs to strings:
  106. * "disk" for images known to be cached in non-volatile storage, "memory" for images known
  107. * to be cached in memory. Dictionary items corresponding to images that are not known to be
  108. * cached are simply missing.
  109. */
  110. - (NSDictionary *)getImageCacheStatus:(NSArray *)requests;
  111. /**
  112. * Allows developers to set their own caching implementation for
  113. * decoded images as long as it conforms to the RCTImageCache
  114. * protocol. This method should be called in bridgeDidInitializeModule.
  115. */
  116. - (void)setImageCache:(id<RCTImageCache>)cache;
  117. @end