UIImage+YYAdd.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. //
  2. // UIImage+YYAdd.h
  3. // YYKit <https://github.com/ibireme/YYKit>
  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 (YYAdd)
  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 *)imageWithSmallGIFData:(NSData *)data scale:(CGFloat)scale;
  35. /**
  36. Whether the data is animated GIF.
  37. @param data Image data
  38. @return Returns YES only if the data is gif and contains more than one frame,
  39. otherwise returns NO.
  40. */
  41. + (BOOL)isAnimatedGIFData:(NSData *)data;
  42. /**
  43. Whether the file in the specified path is GIF.
  44. @param path An absolute file path.
  45. @return Returns YES if the file is gif, otherwise returns NO.
  46. */
  47. + (BOOL)isAnimatedGIFFile:(NSString *)path;
  48. /**
  49. Create an image from a PDF file data or path.
  50. @discussion If the PDF has multiple page, is just return's the first page's
  51. content. Image's scale is equal to current screen's scale, size is same as
  52. PDF's origin size.
  53. @param dataOrPath PDF data in `NSData`, or PDF file path in `NSString`.
  54. @return A new image create from PDF, or nil when an error occurs.
  55. */
  56. + (nullable UIImage *)imageWithPDF:(id)dataOrPath;
  57. /**
  58. Create an image from a PDF file data or path.
  59. @discussion If the PDF has multiple page, is just return's the first page's
  60. content. Image's scale is equal to current screen's scale.
  61. @param dataOrPath PDF data in `NSData`, or PDF file path in `NSString`.
  62. @param size The new image's size, PDF's content will be stretched as needed.
  63. @return A new image create from PDF, or nil when an error occurs.
  64. */
  65. + (nullable UIImage *)imageWithPDF:(id)dataOrPath size:(CGSize)size;
  66. /**
  67. Create a square image from apple emoji.
  68. @discussion It creates a square image from apple emoji, image's scale is equal
  69. to current screen's scale. The original emoji image in `AppleColorEmoji` font
  70. is in size 160*160 px.
  71. @param emoji single emoji, such as @"😄".
  72. @param size image's size.
  73. @return Image from emoji, or nil when an error occurs.
  74. */
  75. + (nullable UIImage *)imageWithEmoji:(NSString *)emoji size:(CGFloat)size;
  76. /**
  77. Create and return a 1x1 point size image with the given color.
  78. @param color The color.
  79. */
  80. + (nullable UIImage *)imageWithColor:(UIColor *)color;
  81. /**
  82. Create and return a pure color image with the given color and size.
  83. @param color The color.
  84. @param size New image's type.
  85. */
  86. + (nullable UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size;
  87. /**
  88. Create and return an image with custom draw code.
  89. @param size The image size.
  90. @param drawBlock The draw block.
  91. @return The new image.
  92. */
  93. + (nullable UIImage *)imageWithSize:(CGSize)size drawBlock:(void (^)(CGContextRef context))drawBlock;
  94. #pragma mark - Image Info
  95. ///=============================================================================
  96. /// @name Image Info
  97. ///=============================================================================
  98. /**
  99. Whether this image has alpha channel.
  100. */
  101. - (BOOL)hasAlphaChannel;
  102. #pragma mark - Modify Image
  103. ///=============================================================================
  104. /// @name Modify Image
  105. ///=============================================================================
  106. /**
  107. Draws the entire image in the specified rectangle, content changed with
  108. the contentMode.
  109. @discussion This method draws the entire image in the current graphics context,
  110. respecting the image's orientation setting. In the default coordinate system,
  111. images are situated down and to the right of the origin of the specified
  112. rectangle. This method respects any transforms applied to the current graphics
  113. context, however.
  114. @param rect The rectangle in which to draw the image.
  115. @param contentMode Draw content mode
  116. @param clips A Boolean value that determines whether content are confined to the rect.
  117. */
  118. - (void)drawInRect:(CGRect)rect withContentMode:(UIViewContentMode)contentMode clipsToBounds:(BOOL)clips;
  119. /**
  120. Returns a new image which is scaled from this image.
  121. The image will be stretched as needed.
  122. @param size The new size to be scaled, values should be positive.
  123. @return The new image with the given size.
  124. */
  125. - (nullable UIImage *)imageByResizeToSize:(CGSize)size;
  126. /**
  127. Returns a new image which is scaled from this image.
  128. The image content will be changed with thencontentMode.
  129. @param size The new size to be scaled, values should be positive.
  130. @param contentMode The content mode for image content.
  131. @return The new image with the given size.
  132. */
  133. - (nullable UIImage *)imageByResizeToSize:(CGSize)size contentMode:(UIViewContentMode)contentMode;
  134. /**
  135. Returns a new image which is cropped from this image.
  136. @param rect Image's inner rect.
  137. @return The new image, or nil if an error occurs.
  138. */
  139. - (nullable UIImage *)imageByCropToRect:(CGRect)rect;
  140. /**
  141. Returns a new image which is edge inset from this image.
  142. @param insets Inset (positive) for each of the edges, values can be negative to 'outset'.
  143. @param color Extend edge's fill color, nil means clear color.
  144. @return The new image, or nil if an error occurs.
  145. */
  146. - (nullable UIImage *)imageByInsetEdge:(UIEdgeInsets)insets withColor:(nullable UIColor *)color;
  147. /**
  148. Rounds a new image with a given corner size.
  149. @param radius The radius of each corner oval. Values larger than half the
  150. rectangle's width or height are clamped appropriately to half
  151. the width or height.
  152. */
  153. - (nullable UIImage *)imageByRoundCornerRadius:(CGFloat)radius;
  154. /**
  155. Rounds a new image with a given corner size.
  156. @param radius The radius of each corner oval. Values larger than half the
  157. rectangle's width or height are clamped appropriately to
  158. half the width or height.
  159. @param borderWidth The inset border line width. Values larger than half the rectangle's
  160. width or height are clamped appropriately to half the width
  161. or height.
  162. @param borderColor The border stroke color. nil means clear color.
  163. */
  164. - (nullable UIImage *)imageByRoundCornerRadius:(CGFloat)radius
  165. borderWidth:(CGFloat)borderWidth
  166. borderColor:(nullable UIColor *)borderColor;
  167. /**
  168. Rounds a new image with a given corner size.
  169. @param radius The radius of each corner oval. Values larger than half the
  170. rectangle's width or height are clamped appropriately to
  171. half the width or height.
  172. @param corners A bitmask value that identifies the corners that you want
  173. rounded. You can use this parameter to round only a subset
  174. of the corners of the rectangle.
  175. @param borderWidth The inset border line width. Values larger than half the rectangle's
  176. width or height are clamped appropriately to half the width
  177. or height.
  178. @param borderColor The border stroke color. nil means clear color.
  179. @param borderLineJoin The border line join.
  180. */
  181. - (nullable UIImage *)imageByRoundCornerRadius:(CGFloat)radius
  182. corners:(UIRectCorner)corners
  183. borderWidth:(CGFloat)borderWidth
  184. borderColor:(nullable UIColor *)borderColor
  185. borderLineJoin:(CGLineJoin)borderLineJoin;
  186. /**
  187. Returns a new rotated image (relative to the center).
  188. @param radians Rotated radians in counterclockwise.⟲
  189. @param fitSize YES: new image's size is extend to fit all content.
  190. NO: image's size will not change, content may be clipped.
  191. */
  192. - (nullable UIImage *)imageByRotate:(CGFloat)radians fitSize:(BOOL)fitSize;
  193. /**
  194. Returns a new image rotated counterclockwise by a quarter‑turn (90°). ⤺
  195. The width and height will be exchanged.
  196. */
  197. - (nullable UIImage *)imageByRotateLeft90;
  198. /**
  199. Returns a new image rotated clockwise by a quarter‑turn (90°). ⤼
  200. The width and height will be exchanged.
  201. */
  202. - (nullable UIImage *)imageByRotateRight90;
  203. /**
  204. Returns a new image rotated 180° . ↻
  205. */
  206. - (nullable UIImage *)imageByRotate180;
  207. /**
  208. Returns a vertically flipped image. ⥯
  209. */
  210. - (nullable UIImage *)imageByFlipVertical;
  211. /**
  212. Returns a horizontally flipped image. ⇋
  213. */
  214. - (nullable UIImage *)imageByFlipHorizontal;
  215. #pragma mark - Image Effect
  216. ///=============================================================================
  217. /// @name Image Effect
  218. ///=============================================================================
  219. /**
  220. Tint the image in alpha channel with the given color.
  221. @param color The color.
  222. */
  223. - (nullable UIImage *)imageByTintColor:(UIColor *)color;
  224. /**
  225. Returns a grayscaled image.
  226. */
  227. - (nullable UIImage *)imageByGrayscale;
  228. /**
  229. Applies a blur effect to this image. Suitable for blur any content.
  230. */
  231. - (nullable UIImage *)imageByBlurSoft;
  232. /**
  233. Applies a blur effect to this image. Suitable for blur any content except pure white.
  234. (same as iOS Control Panel)
  235. */
  236. - (nullable UIImage *)imageByBlurLight;
  237. /**
  238. Applies a blur effect to this image. Suitable for displaying black text.
  239. (same as iOS Navigation Bar White)
  240. */
  241. - (nullable UIImage *)imageByBlurExtraLight;
  242. /**
  243. Applies a blur effect to this image. Suitable for displaying white text.
  244. (same as iOS Notification Center)
  245. */
  246. - (nullable UIImage *)imageByBlurDark;
  247. /**
  248. Applies a blur and tint color to this image.
  249. @param tintColor The tint color.
  250. */
  251. - (nullable UIImage *)imageByBlurWithTint:(UIColor *)tintColor;
  252. /**
  253. Applies a blur, tint color, and saturation adjustment to this image,
  254. optionally within the area specified by @a maskImage.
  255. @param blurRadius The radius of the blur in points, 0 means no blur effect.
  256. @param tintColor An optional UIColor object that is uniformly blended with
  257. the result of the blur and saturation operations. The
  258. alpha channel of this color determines how strong the
  259. tint is. nil means no tint.
  260. @param tintBlendMode The @a tintColor blend mode. Default is kCGBlendModeNormal (0).
  261. @param saturation A value of 1.0 produces no change in the resulting image.
  262. Values less than 1.0 will desaturation the resulting image
  263. while values greater than 1.0 will have the opposite effect.
  264. 0 means gray scale.
  265. @param maskImage If specified, @a inputImage is only modified in the area(s)
  266. defined by this mask. This must be an image mask or it
  267. must meet the requirements of the mask parameter of
  268. CGContextClipToMask.
  269. @return image with effect, or nil if an error occurs (e.g. no
  270. enough memory).
  271. */
  272. - (nullable UIImage *)imageByBlurRadius:(CGFloat)blurRadius
  273. tintColor:(nullable UIColor *)tintColor
  274. tintMode:(CGBlendMode)tintBlendMode
  275. saturation:(CGFloat)saturation
  276. maskImage:(nullable UIImage *)maskImage;
  277. @end
  278. NS_ASSUME_NONNULL_END