UIImage+YYWebImage.h 9.1 KB

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