UIImage+YYWebImage.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. // UIImage+YYWebImage.h
  3. // YYWebImage <https://github.com/ibireme/YYWebImage>
  4. //
  5. // Created by ibireme on 13/4/4.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import <UIKit/UIKit.h>
  12. NS_ASSUME_NONNULL_BEGIN
  13. /**
  14. Provide some commen method for `UIImage`.
  15. Image process is based on CoreGraphic and vImage.
  16. */
  17. @interface UIImage (YYWebImage)
  18. #pragma mark - Create image
  19. ///=============================================================================
  20. /// @name Create image
  21. ///=============================================================================
  22. /**
  23. Create an animated image with GIF data. After created, you can access
  24. the images via property '.images'. If the data is not animated gif, this
  25. function is same as [UIImage imageWithData:data scale:scale];
  26. @discussion It has a better display performance, but costs more memory
  27. (width * height * frames Bytes). It only suited to display small
  28. gif such as animated emoticon. If you want to display large gif,
  29. see `YYImage`.
  30. @param data GIF data.
  31. @param scale The scale factor
  32. @return A new image created from GIF, or nil when an error occurs.
  33. */
  34. + (nullable UIImage *)yy_imageWithSmallGIFData:(NSData *)data scale:(CGFloat)scale;
  35. /**
  36. Create and return a 1x1 point size image with the given color.
  37. @param color The color.
  38. */
  39. + (nullable UIImage *)yy_imageWithColor:(UIColor *)color;
  40. /**
  41. Create and return a pure color image with the given color and size.
  42. @param color The color.
  43. @param size New image's type.
  44. */
  45. + (nullable UIImage *)yy_imageWithColor:(UIColor *)color size:(CGSize)size;
  46. /**
  47. Create and return an image with custom draw code.
  48. @param size The image size.
  49. @param drawBlock The draw block.
  50. @return The new image.
  51. */
  52. + (nullable UIImage *)yy_imageWithSize:(CGSize)size drawBlock:(void (^)(CGContextRef context))drawBlock;
  53. #pragma mark - Image Info
  54. ///=============================================================================
  55. /// @name Image Info
  56. ///=============================================================================
  57. /**
  58. Whether this image has alpha channel.
  59. */
  60. - (BOOL)yy_hasAlphaChannel;
  61. #pragma mark - Modify Image
  62. ///=============================================================================
  63. /// @name Modify Image
  64. ///=============================================================================
  65. /**
  66. Draws the entire image in the specified rectangle, content changed with
  67. the contentMode.
  68. @discussion This method draws the entire image in the current graphics context,
  69. respecting the image's orientation setting. In the default coordinate system,
  70. images are situated down and to the right of the origin of the specified
  71. rectangle. This method respects any transforms applied to the current graphics
  72. context, however.
  73. @param rect The rectangle in which to draw the image.
  74. @param contentMode Draw content mode
  75. @param clips A Boolean value that determines whether content are confined to the rect.
  76. */
  77. - (void)yy_drawInRect:(CGRect)rect withContentMode:(UIViewContentMode)contentMode clipsToBounds:(BOOL)clips;
  78. /**
  79. Returns a new image which is scaled from this image.
  80. The image will be stretched as needed.
  81. @param size The new size to be scaled, values should be positive.
  82. @return The new image with the given size.
  83. */
  84. - (nullable UIImage *)yy_imageByResizeToSize:(CGSize)size;
  85. /**
  86. Returns a new image which is scaled from this image.
  87. The image content will be changed with thencontentMode.
  88. @param size The new size to be scaled, values should be positive.
  89. @param contentMode The content mode for image content.
  90. @return The new image with the given size.
  91. */
  92. - (nullable UIImage *)yy_imageByResizeToSize:(CGSize)size contentMode:(UIViewContentMode)contentMode;
  93. /**
  94. Returns a new image which is cropped from this image.
  95. @param rect Image's inner rect.
  96. @return The new image, or nil if an error occurs.
  97. */
  98. - (nullable UIImage *)yy_imageByCropToRect:(CGRect)rect;
  99. /**
  100. Returns a new image which is edge inset from this image.
  101. @param insets Inset (positive) for each of the edges, values can be negative to 'outset'.
  102. @param color Extend edge's fill color, nil means clear color.
  103. @return The new image, or nil if an error occurs.
  104. */
  105. - (nullable UIImage *)yy_imageByInsetEdge:(UIEdgeInsets)insets withColor:(nullable UIColor *)color;
  106. /**
  107. Rounds a new image with a given corner size.
  108. @param radius The radius of each corner oval. Values larger than half the
  109. rectangle's width or height are clamped appropriately to half
  110. the width or height.
  111. */
  112. - (nullable UIImage *)yy_imageByRoundCornerRadius:(CGFloat)radius;
  113. /**
  114. Rounds a new image with a given corner size.
  115. @param radius The radius of each corner oval. Values larger than half the
  116. rectangle's width or height are clamped appropriately to
  117. half the width or height.
  118. @param borderWidth The inset border line width. Values larger than half the rectangle's
  119. width or height are clamped appropriately to half the width
  120. or height.
  121. @param borderColor The border stroke color. nil means clear color.
  122. */
  123. - (nullable UIImage *)yy_imageByRoundCornerRadius:(CGFloat)radius
  124. borderWidth:(CGFloat)borderWidth
  125. borderColor:(nullable UIColor *)borderColor;
  126. /**
  127. Rounds a new image with a given corner size.
  128. @param radius The radius of each corner oval. Values larger than half the
  129. rectangle's width or height are clamped appropriately to
  130. half the width or height.
  131. @param corners A bitmask value that identifies the corners that you want
  132. rounded. You can use this parameter to round only a subset
  133. of the corners of the rectangle.
  134. @param borderWidth The inset border line width. Values larger than half the rectangle's
  135. width or height are clamped appropriately to half the width
  136. or height.
  137. @param borderColor The border stroke color. nil means clear color.
  138. @param borderLineJoin The border line join.
  139. */
  140. - (nullable UIImage *)yy_imageByRoundCornerRadius:(CGFloat)radius
  141. corners:(UIRectCorner)corners
  142. borderWidth:(CGFloat)borderWidth
  143. borderColor:(nullable UIColor *)borderColor
  144. borderLineJoin:(CGLineJoin)borderLineJoin;
  145. /**
  146. Returns a new rotated image (relative to the center).
  147. @param radians Rotated radians in counterclockwise.⟲
  148. @param fitSize YES: new image's size is extend to fit all content.
  149. NO: image's size will not change, content may be clipped.
  150. */
  151. - (nullable UIImage *)yy_imageByRotate:(CGFloat)radians fitSize:(BOOL)fitSize;
  152. /**
  153. Returns a new image rotated counterclockwise by a quarter‑turn (90°). ⤺
  154. The width and height will be exchanged.
  155. */
  156. - (nullable UIImage *)yy_imageByRotateLeft90;
  157. /**
  158. Returns a new image rotated clockwise by a quarter‑turn (90°). ⤼
  159. The width and height will be exchanged.
  160. */
  161. - (nullable UIImage *)yy_imageByRotateRight90;
  162. /**
  163. Returns a new image rotated 180° . ↻
  164. */
  165. - (nullable UIImage *)yy_imageByRotate180;
  166. /**
  167. Returns a vertically flipped image. ⥯
  168. */
  169. - (nullable UIImage *)yy_imageByFlipVertical;
  170. /**
  171. Returns a horizontally flipped image. ⇋
  172. */
  173. - (nullable UIImage *)yy_imageByFlipHorizontal;
  174. #pragma mark - Image Effect
  175. ///=============================================================================
  176. /// @name Image Effect
  177. ///=============================================================================
  178. /**
  179. Tint the image in alpha channel with the given color.
  180. @param color The color.
  181. */
  182. - (nullable UIImage *)yy_imageByTintColor:(UIColor *)color;
  183. /**
  184. Returns a grayscaled image.
  185. */
  186. - (nullable UIImage *)yy_imageByGrayscale;
  187. /**
  188. Applies a blur effect to this image. Suitable for blur any content.
  189. */
  190. - (nullable UIImage *)yy_imageByBlurSoft;
  191. /**
  192. Applies a blur effect to this image. Suitable for blur any content except pure white.
  193. (same as iOS Control Panel)
  194. */
  195. - (nullable UIImage *)yy_imageByBlurLight;
  196. /**
  197. Applies a blur effect to this image. Suitable for displaying black text.
  198. (same as iOS Navigation Bar White)
  199. */
  200. - (nullable UIImage *)yy_imageByBlurExtraLight;
  201. /**
  202. Applies a blur effect to this image. Suitable for displaying white text.
  203. (same as iOS Notification Center)
  204. */
  205. - (nullable UIImage *)yy_imageByBlurDark;
  206. /**
  207. Applies a blur and tint color to this image.
  208. @param tintColor The tint color.
  209. */
  210. - (nullable UIImage *)yy_imageByBlurWithTint:(UIColor *)tintColor;
  211. /**
  212. Applies a blur, tint color, and saturation adjustment to this image,
  213. optionally within the area specified by @a maskImage.
  214. @param blurRadius The radius of the blur in points, 0 means no blur effect.
  215. @param tintColor An optional UIColor object that is uniformly blended with
  216. the result of the blur and saturation operations. The
  217. alpha channel of this color determines how strong the
  218. tint is. nil means no tint.
  219. @param tintBlendMode The @a tintColor blend mode. Default is kCGBlendModeNormal (0).
  220. @param saturation A value of 1.0 produces no change in the resulting image.
  221. Values less than 1.0 will desaturation the resulting image
  222. while values greater than 1.0 will have the opposite effect.
  223. 0 means gray scale.
  224. @param maskImage If specified, @a inputImage is only modified in the area(s)
  225. defined by this mask. This must be an image mask or it
  226. must meet the requirements of the mask parameter of
  227. CGContextClipToMask.
  228. @return image with effect, or nil if an error occurs (e.g. no
  229. enough memory).
  230. */
  231. - (nullable UIImage *)yy_imageByBlurRadius:(CGFloat)blurRadius
  232. tintColor:(nullable UIColor *)tintColor
  233. tintMode:(CGBlendMode)tintBlendMode
  234. saturation:(CGFloat)saturation
  235. maskImage:(nullable UIImage *)maskImage;
  236. @end
  237. NS_ASSUME_NONNULL_END