SDImageTransformer.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import "SDWebImageCompat.h"
  9. #import "UIImage+Transform.h"
  10. /**
  11. Return the transformed cache key which applied with specify transformerKey.
  12. @param key The original cache key
  13. @param transformerKey The transformer key from the transformer
  14. @return The transformed cache key
  15. */
  16. FOUNDATION_EXPORT NSString * _Nullable SDTransformedKeyForKey(NSString * _Nullable key, NSString * _Nonnull transformerKey);
  17. /**
  18. Return the thumbnailed cache key which applied with specify thumbnailSize and preserveAspectRatio control.
  19. @param key The original cache key
  20. @param thumbnailPixelSize The thumbnail pixel size
  21. @param preserveAspectRatio The preserve aspect ratio option
  22. @return The thumbnailed cache key
  23. @note If you have both transformer and thumbnail applied for image, call `SDThumbnailedKeyForKey` firstly and then with `SDTransformedKeyForKey`.`
  24. */
  25. FOUNDATION_EXPORT NSString * _Nullable SDThumbnailedKeyForKey(NSString * _Nullable key, CGSize thumbnailPixelSize, BOOL preserveAspectRatio);
  26. /**
  27. A transformer protocol to transform the image load from cache or from download.
  28. You can provide transformer to cache and manager (Through the `transformer` property or context option `SDWebImageContextImageTransformer`).
  29. @note The transform process is called from a global queue in order to not to block the main queue.
  30. */
  31. @protocol SDImageTransformer <NSObject>
  32. @required
  33. /**
  34. For each transformer, it must contains its cache key to used to store the image cache or query from the cache. This key will be appened after the original cache key generated by URL or from user.
  35. @return The cache key to appended after the original cache key. Should not be nil.
  36. */
  37. @property (nonatomic, copy, readonly, nonnull) NSString *transformerKey;
  38. /**
  39. Transform the image to another image.
  40. @param image The image to be transformed
  41. @param key The cache key associated to the image. This arg is a hint for image source, not always useful and should be nullable. In the future we will remove this arg.
  42. @return The transformed image, or nil if transform failed
  43. */
  44. - (nullable UIImage *)transformedImageWithImage:(nonnull UIImage *)image forKey:(nonnull NSString *)key API_DEPRECATED("The key arg will be removed in the future. Update your code and don't rely on that.", macos(10.10, API_TO_BE_DEPRECATED), ios(8.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED));
  45. @end
  46. #pragma mark - Pipeline
  47. /**
  48. Pipeline transformer. Which you can bind multiple transformers together to let the image to be transformed one by one in order and generate the final image.
  49. @note Because transformers are lightweight, if you want to append or arrange transformers, create another pipeline transformer instead. This class is considered as immutable.
  50. */
  51. @interface SDImagePipelineTransformer : NSObject <SDImageTransformer>
  52. /**
  53. All transformers in pipeline
  54. */
  55. @property (nonatomic, copy, readonly, nonnull) NSArray<id<SDImageTransformer>> *transformers;
  56. - (nonnull instancetype)init NS_UNAVAILABLE;
  57. + (nonnull instancetype)transformerWithTransformers:(nonnull NSArray<id<SDImageTransformer>> *)transformers;
  58. @end
  59. // There are some built-in transformers based on the `UIImage+Transformer` category to provide the common image geometry, image blending and image effect process. Those transform are useful for static image only but you can create your own to support animated image as well.
  60. // Because transformers are lightweight, these class are considered as immutable.
  61. #pragma mark - Image Geometry
  62. /**
  63. Image round corner transformer
  64. */
  65. @interface SDImageRoundCornerTransformer: NSObject <SDImageTransformer>
  66. /**
  67. The radius of each corner oval. Values larger than half the
  68. rectangle's width or height are clamped appropriately to
  69. half the width or height.
  70. */
  71. @property (nonatomic, assign, readonly) CGFloat cornerRadius;
  72. /**
  73. A bitmask value that identifies the corners that you want
  74. rounded. You can use this parameter to round only a subset
  75. of the corners of the rectangle.
  76. */
  77. @property (nonatomic, assign, readonly) SDRectCorner corners;
  78. /**
  79. The inset border line width. Values larger than half the rectangle's
  80. width or height are clamped appropriately to half the width
  81. or height.
  82. */
  83. @property (nonatomic, assign, readonly) CGFloat borderWidth;
  84. /**
  85. The border stroke color. nil means clear color.
  86. */
  87. @property (nonatomic, strong, readonly, nullable) UIColor *borderColor;
  88. - (nonnull instancetype)init NS_UNAVAILABLE;
  89. + (nonnull instancetype)transformerWithRadius:(CGFloat)cornerRadius corners:(SDRectCorner)corners borderWidth:(CGFloat)borderWidth borderColor:(nullable UIColor *)borderColor;
  90. @end
  91. /**
  92. Image resizing transformer
  93. */
  94. @interface SDImageResizingTransformer : NSObject <SDImageTransformer>
  95. /**
  96. The new size to be resized, values should be positive.
  97. */
  98. @property (nonatomic, assign, readonly) CGSize size;
  99. /**
  100. The scale mode for image content.
  101. */
  102. @property (nonatomic, assign, readonly) SDImageScaleMode scaleMode;
  103. - (nonnull instancetype)init NS_UNAVAILABLE;
  104. + (nonnull instancetype)transformerWithSize:(CGSize)size scaleMode:(SDImageScaleMode)scaleMode;
  105. @end
  106. /**
  107. Image cropping transformer
  108. */
  109. @interface SDImageCroppingTransformer : NSObject <SDImageTransformer>
  110. /**
  111. Image's inner rect.
  112. */
  113. @property (nonatomic, assign, readonly) CGRect rect;
  114. - (nonnull instancetype)init NS_UNAVAILABLE;
  115. + (nonnull instancetype)transformerWithRect:(CGRect)rect;
  116. @end
  117. /**
  118. Image flipping transformer
  119. */
  120. @interface SDImageFlippingTransformer : NSObject <SDImageTransformer>
  121. /**
  122. YES to flip the image horizontally. ⇋
  123. */
  124. @property (nonatomic, assign, readonly) BOOL horizontal;
  125. /**
  126. YES to flip the image vertically. ⥯
  127. */
  128. @property (nonatomic, assign, readonly) BOOL vertical;
  129. - (nonnull instancetype)init NS_UNAVAILABLE;
  130. + (nonnull instancetype)transformerWithHorizontal:(BOOL)horizontal vertical:(BOOL)vertical;
  131. @end
  132. /**
  133. Image rotation transformer
  134. */
  135. @interface SDImageRotationTransformer : NSObject <SDImageTransformer>
  136. /**
  137. Rotated radians in counterclockwise.⟲
  138. */
  139. @property (nonatomic, assign, readonly) CGFloat angle;
  140. /**
  141. YES: new image's size is extend to fit all content.
  142. NO: image's size will not change, content may be clipped.
  143. */
  144. @property (nonatomic, assign, readonly) BOOL fitSize;
  145. - (nonnull instancetype)init NS_UNAVAILABLE;
  146. + (nonnull instancetype)transformerWithAngle:(CGFloat)angle fitSize:(BOOL)fitSize;
  147. @end
  148. #pragma mark - Image Blending
  149. /**
  150. Image tint color transformer
  151. */
  152. @interface SDImageTintTransformer : NSObject <SDImageTransformer>
  153. /**
  154. The tint color.
  155. */
  156. @property (nonatomic, strong, readonly, nonnull) UIColor *tintColor;
  157. - (nonnull instancetype)init NS_UNAVAILABLE;
  158. + (nonnull instancetype)transformerWithColor:(nonnull UIColor *)tintColor;
  159. @end
  160. #pragma mark - Image Effect
  161. /**
  162. Image blur effect transformer
  163. */
  164. @interface SDImageBlurTransformer : NSObject <SDImageTransformer>
  165. /**
  166. The radius of the blur in points, 0 means no blur effect.
  167. */
  168. @property (nonatomic, assign, readonly) CGFloat blurRadius;
  169. - (nonnull instancetype)init NS_UNAVAILABLE;
  170. + (nonnull instancetype)transformerWithRadius:(CGFloat)blurRadius;
  171. @end
  172. #if SD_UIKIT || SD_MAC
  173. /**
  174. Core Image filter transformer
  175. */
  176. @interface SDImageFilterTransformer: NSObject <SDImageTransformer>
  177. /**
  178. The CIFilter to be applied to the image.
  179. */
  180. @property (nonatomic, strong, readonly, nonnull) CIFilter *filter;
  181. - (nonnull instancetype)init NS_UNAVAILABLE;
  182. + (nonnull instancetype)transformerWithFilter:(nonnull CIFilter *)filter;
  183. @end
  184. #endif