UIImage+Additions.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // UIImage+Additions.h
  3. // Created by Joan Martin.
  4. // Take a look to my repos at http://github.com/vilanovi
  5. //
  6. // Copyright (c) 2013 Joan Martin, vilanovi@gmail.com.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights to
  11. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  12. // of the Software, and to permit persons to whom the Software is furnished to do
  13. // so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in all
  16. // copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  20. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
  24. #import <UIKit/UIKit.h>
  25. typedef struct __UICornerInset
  26. {
  27. CGFloat topLeft;
  28. CGFloat topRight;
  29. CGFloat bottomLeft;
  30. CGFloat bottomRight;
  31. } UICornerInset;
  32. UIKIT_EXTERN const UICornerInset UICornerInsetZero;
  33. UIKIT_STATIC_INLINE UICornerInset UICornerInsetMake(CGFloat topLeft, CGFloat topRight, CGFloat bottomLeft, CGFloat bottomRight)
  34. {
  35. UICornerInset cornerInset = {topLeft, topRight, bottomLeft, bottomRight};
  36. return cornerInset;
  37. }
  38. UIKIT_STATIC_INLINE UICornerInset UICornerInsetMakeWithRadius(CGFloat radius)
  39. {
  40. UICornerInset cornerInset = {radius, radius, radius, radius};
  41. return cornerInset;
  42. }
  43. UIKIT_STATIC_INLINE BOOL UICornerInsetEqualToCornerInset(UICornerInset cornerInset1, UICornerInset cornerInset2)
  44. {
  45. return
  46. cornerInset1.topLeft == cornerInset2.topLeft &&
  47. cornerInset1.topRight == cornerInset2.topRight &&
  48. cornerInset1.bottomLeft == cornerInset2.bottomLeft &&
  49. cornerInset1.bottomRight == cornerInset2.bottomRight;
  50. }
  51. FOUNDATION_EXTERN NSString* NSStringFromUICornerInset(UICornerInset cornerInset);
  52. typedef enum __UIImageTintedStyle
  53. {
  54. UIImageTintedStyleKeepingAlpha = 1,
  55. UIImageTintedStyleOverAlpha = 2
  56. } UIImageTintedStyle;
  57. typedef enum __UIImageGradientDirection
  58. {
  59. UIImageGradientDirectionVertical = 1,
  60. UIImageGradientDirectionHorizontal = 2,
  61. } UIImageGradientDirection;
  62. @interface UIImage (Additions)
  63. /*
  64. * Create images from colors
  65. */
  66. + (UIImage*)imageWithColor:(UIColor*)color size:(CGSize)size;
  67. + (UIImage*)imageWithColor:(UIColor*)color size:(CGSize)size cornerRadius:(CGFloat)cornerRadius;
  68. + (UIImage*)imageWithColor:(UIColor*)color size:(CGSize)size cornerInset:(UICornerInset)cornerInset;
  69. /*
  70. * Create rezisable images from colors
  71. */
  72. + (UIImage*)resizableImageWithColor:(UIColor*)color;
  73. + (UIImage*)resizableImageWithColor:(UIColor*)color cornerRadius:(CGFloat)cornerRadius;
  74. + (UIImage*)resizableImageWithColor:(UIColor*)color cornerInset:(UICornerInset)cornerInset;
  75. + (UIImage*)blackColorImage;
  76. + (UIImage*)darkGrayColorImage;
  77. + (UIImage*)lightGrayColorImage;
  78. + (UIImage*)whiteColorImage;
  79. + (UIImage*)grayColorImage;
  80. + (UIImage*)redColorImage;
  81. + (UIImage*)greenColorImage;
  82. + (UIImage*)blueColorImage;
  83. + (UIImage*)cyanColorImage;
  84. + (UIImage*)yellowColorImage;
  85. + (UIImage*)magentaColorImage;
  86. + (UIImage*)orangeColorImage;
  87. + (UIImage*)purpleColorImage;
  88. + (UIImage*)brownColorImage;
  89. + (UIImage*)clearColorImage;
  90. /*
  91. * Tint Images
  92. */
  93. + (UIImage*)imageNamed:(NSString *)name tintColor:(UIColor*)color style:(UIImageTintedStyle)tintStyle;
  94. - (UIImage*)tintedImageWithColor:(UIColor*)color style:(UIImageTintedStyle)tintStyle;
  95. /*
  96. * Rounding corners
  97. */
  98. - (UIImage*)imageWithRoundedBounds;
  99. - (UIImage*)imageWithCornerRadius:(CGFloat)cornerRadius;
  100. - (UIImage*)imageWithCornerInset:(UICornerInset)cornerInset;
  101. - (BOOL)isValidCornerInset:(UICornerInset)cornerInset;
  102. /*
  103. * Drawing image on image
  104. */
  105. - (UIImage*)imageAddingImage:(UIImage*)image;
  106. - (UIImage*)imageAddingImage:(UIImage*)image offset:(CGPoint)offset;
  107. /*
  108. * Gradient image generation
  109. */
  110. + (UIImage*)imageWithGradient:(NSArray*)colors size:(CGSize)size direction:(UIImageGradientDirection)direction;
  111. + (UIImage*)resizableImageWithGradient:(NSArray*)colors size:(CGSize)size direction:(UIImageGradientDirection)direction;
  112. /*
  113. * tint只对里面的图案作更改颜色操作
  114. */
  115. - (UIImage *)imageWithTintColor:(UIColor *)tintColor;
  116. - (UIImage *)imageWithTintColor:(UIColor *)tintColor blendMode:(CGBlendMode)blendMode;
  117. - (UIImage *)imageWithGradientTintColor:(UIColor *)tintColor;
  118. @end
  119. #pragma mark - Categories
  120. @interface NSValue (UICornerInset)
  121. + (NSValue*)valueWithUICornerInset:(UICornerInset)cornerInset;
  122. - (UICornerInset)UICornerInsetValue;
  123. @end