SDImageTransformer.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. A transformer protocol to transform the image load from cache or from download.
  19. You can provide transformer to cache and manager (Through the `transformer` property or context option `SDWebImageContextImageTransformer`).
  20. @note The transform process is called from a global queue in order to not to block the main queue.
  21. */
  22. @protocol SDImageTransformer <NSObject>
  23. @required
  24. /**
  25. 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.
  26. @return The cache key to appended after the original cache key. Should not be nil.
  27. */
  28. @property (nonatomic, copy, readonly, nonnull) NSString *transformerKey;
  29. /**
  30. Transform the image to another image.
  31. @param image The image to be transformed
  32. @param key The cache key associated to the image
  33. @return The transformed image, or nil if transform failed
  34. */
  35. - (nullable UIImage *)transformedImageWithImage:(nonnull UIImage *)image forKey:(nonnull NSString *)key;
  36. @end
  37. #pragma mark - Pipeline
  38. /**
  39. 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.
  40. @note Because transformers are lightweight, if you want to append or arrange transfomers, create another pipeline transformer instead. This class is considered as immutable.
  41. */
  42. @interface SDImagePipelineTransformer : NSObject <SDImageTransformer>
  43. /**
  44. All transformers in pipeline
  45. */
  46. @property (nonatomic, copy, readonly, nonnull) NSArray<id<SDImageTransformer>> *transformers;
  47. - (nonnull instancetype)init NS_UNAVAILABLE;
  48. + (nonnull instancetype)transformerWithTransformers:(nonnull NSArray<id<SDImageTransformer>> *)transformers;
  49. @end
  50. // 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.
  51. // Because transformers are lightweight, these class are considered as immutable.
  52. #pragma mark - Image Geometry
  53. /**
  54. Image round corner transformer
  55. */
  56. @interface SDImageRoundCornerTransformer: NSObject <SDImageTransformer>
  57. /**
  58. The radius of each corner oval. Values larger than half the
  59. rectangle's width or height are clamped appropriately to
  60. half the width or height.
  61. */
  62. @property (nonatomic, assign, readonly) CGFloat cornerRadius;
  63. /**
  64. A bitmask value that identifies the corners that you want
  65. rounded. You can use this parameter to round only a subset
  66. of the corners of the rectangle.
  67. */
  68. @property (nonatomic, assign, readonly) SDRectCorner corners;
  69. /**
  70. The inset border line width. Values larger than half the rectangle's
  71. width or height are clamped appropriately to half the width
  72. or height.
  73. */
  74. @property (nonatomic, assign, readonly) CGFloat borderWidth;
  75. /**
  76. The border stroke color. nil means clear color.
  77. */
  78. @property (nonatomic, strong, readonly, nullable) UIColor *borderColor;
  79. - (nonnull instancetype)init NS_UNAVAILABLE;
  80. + (nonnull instancetype)transformerWithRadius:(CGFloat)cornerRadius corners:(SDRectCorner)corners borderWidth:(CGFloat)borderWidth borderColor:(nullable UIColor *)borderColor;
  81. @end
  82. /**
  83. Image resizing transformer
  84. */
  85. @interface SDImageResizingTransformer : NSObject <SDImageTransformer>
  86. /**
  87. The new size to be resized, values should be positive.
  88. */
  89. @property (nonatomic, assign, readonly) CGSize size;
  90. /**
  91. The scale mode for image content.
  92. */
  93. @property (nonatomic, assign, readonly) SDImageScaleMode scaleMode;
  94. - (nonnull instancetype)init NS_UNAVAILABLE;
  95. + (nonnull instancetype)transformerWithSize:(CGSize)size scaleMode:(SDImageScaleMode)scaleMode;
  96. @end
  97. /**
  98. Image cropping transformer
  99. */
  100. @interface SDImageCroppingTransformer : NSObject <SDImageTransformer>
  101. /**
  102. Image's inner rect.
  103. */
  104. @property (nonatomic, assign, readonly) CGRect rect;
  105. - (nonnull instancetype)init NS_UNAVAILABLE;
  106. + (nonnull instancetype)transformerWithRect:(CGRect)rect;
  107. @end
  108. /**
  109. Image flipping transformer
  110. */
  111. @interface SDImageFlippingTransformer : NSObject <SDImageTransformer>
  112. /**
  113. YES to flip the image horizontally. ⇋
  114. */
  115. @property (nonatomic, assign, readonly) BOOL horizontal;
  116. /**
  117. YES to flip the image vertically. ⥯
  118. */
  119. @property (nonatomic, assign, readonly) BOOL vertical;
  120. - (nonnull instancetype)init NS_UNAVAILABLE;
  121. + (nonnull instancetype)transformerWithHorizontal:(BOOL)horizontal vertical:(BOOL)vertical;
  122. @end
  123. /**
  124. Image rotation transformer
  125. */
  126. @interface SDImageRotationTransformer : NSObject <SDImageTransformer>
  127. /**
  128. Rotated radians in counterclockwise.⟲
  129. */
  130. @property (nonatomic, assign, readonly) CGFloat angle;
  131. /**
  132. YES: new image's size is extend to fit all content.
  133. NO: image's size will not change, content may be clipped.
  134. */
  135. @property (nonatomic, assign, readonly) BOOL fitSize;
  136. - (nonnull instancetype)init NS_UNAVAILABLE;
  137. + (nonnull instancetype)transformerWithAngle:(CGFloat)angle fitSize:(BOOL)fitSize;
  138. @end
  139. #pragma mark - Image Blending
  140. /**
  141. Image tint color transformer
  142. */
  143. @interface SDImageTintTransformer : NSObject <SDImageTransformer>
  144. /**
  145. The tint color.
  146. */
  147. @property (nonatomic, strong, readonly, nonnull) UIColor *tintColor;
  148. - (nonnull instancetype)init NS_UNAVAILABLE;
  149. + (nonnull instancetype)transformerWithColor:(nonnull UIColor *)tintColor;
  150. @end
  151. #pragma mark - Image Effect
  152. /**
  153. Image blur effect transformer
  154. */
  155. @interface SDImageBlurTransformer : NSObject <SDImageTransformer>
  156. /**
  157. The radius of the blur in points, 0 means no blur effect.
  158. */
  159. @property (nonatomic, assign, readonly) CGFloat blurRadius;
  160. - (nonnull instancetype)init NS_UNAVAILABLE;
  161. + (nonnull instancetype)transformerWithRadius:(CGFloat)blurRadius;
  162. @end
  163. #if SD_UIKIT || SD_MAC
  164. /**
  165. Core Image filter transformer
  166. */
  167. @interface SDImageFilterTransformer: NSObject <SDImageTransformer>
  168. /**
  169. The CIFilter to be applied to the image.
  170. */
  171. @property (nonatomic, strong, readonly, nonnull) CIFilter *filter;
  172. - (nonnull instancetype)init NS_UNAVAILABLE;
  173. + (nonnull instancetype)transformerWithFilter:(nonnull CIFilter *)filter;
  174. @end
  175. #endif