UIImage+Transform.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. typedef NS_ENUM(NSUInteger, SDImageScaleMode) {
  10. SDImageScaleModeFill = 0,
  11. SDImageScaleModeAspectFit = 1,
  12. SDImageScaleModeAspectFill = 2
  13. };
  14. #if SD_UIKIT || SD_WATCH
  15. typedef UIRectCorner SDRectCorner;
  16. #else
  17. typedef NS_OPTIONS(NSUInteger, SDRectCorner) {
  18. SDRectCornerTopLeft = 1 << 0,
  19. SDRectCornerTopRight = 1 << 1,
  20. SDRectCornerBottomLeft = 1 << 2,
  21. SDRectCornerBottomRight = 1 << 3,
  22. SDRectCornerAllCorners = ~0UL
  23. };
  24. #endif
  25. /**
  26. Provide some common method for `UIImage`.
  27. Image process is based on Core Graphics and vImage.
  28. */
  29. @interface UIImage (Transform)
  30. #pragma mark - Image Geometry
  31. /**
  32. Returns a new image which is resized from this image.
  33. You can specify a larger or smaller size than the image size. The image content will be changed with the scale mode.
  34. @param size The new size to be resized, values should be positive.
  35. @param scaleMode The scale mode for image content.
  36. @return The new image with the given size.
  37. */
  38. - (nullable UIImage *)sd_resizedImageWithSize:(CGSize)size scaleMode:(SDImageScaleMode)scaleMode;
  39. /**
  40. Returns a new image which is cropped from this image.
  41. @param rect Image's inner rect.
  42. @return The new image with the cropping rect.
  43. */
  44. - (nullable UIImage *)sd_croppedImageWithRect:(CGRect)rect;
  45. /**
  46. Rounds a new image with a given corner radius and corners.
  47. @param cornerRadius The radius of each corner oval. Values larger than half the
  48. rectangle's width or height are clamped appropriately to
  49. half the width or height.
  50. @param corners A bitmask value that identifies the corners that you want
  51. rounded. You can use this parameter to round only a subset
  52. of the corners of the rectangle.
  53. @param borderWidth The inset border line width. Values larger than half the rectangle's
  54. width or height are clamped appropriately to half the width
  55. or height.
  56. @param borderColor The border stroke color. nil means clear color.
  57. @return The new image with the round corner.
  58. */
  59. - (nullable UIImage *)sd_roundedCornerImageWithRadius:(CGFloat)cornerRadius
  60. corners:(SDRectCorner)corners
  61. borderWidth:(CGFloat)borderWidth
  62. borderColor:(nullable UIColor *)borderColor;
  63. /**
  64. Returns a new rotated image (relative to the center).
  65. @param angle Rotated radians in counterclockwise.⟲
  66. @param fitSize YES: new image's size is extend to fit all content.
  67. NO: image's size will not change, content may be clipped.
  68. @return The new image with the rotation.
  69. */
  70. - (nullable UIImage *)sd_rotatedImageWithAngle:(CGFloat)angle fitSize:(BOOL)fitSize;
  71. /**
  72. Returns a new horizontally(vertically) flipped image.
  73. @param horizontal YES to flip the image horizontally. ⇋
  74. @param vertical YES to flip the image vertically. ⥯
  75. @return The new image with the flipping.
  76. */
  77. - (nullable UIImage *)sd_flippedImageWithHorizontal:(BOOL)horizontal vertical:(BOOL)vertical;
  78. #pragma mark - Image Blending
  79. /**
  80. Return a tinted image with the given color. This actually use alpha blending of current image and the tint color.
  81. @param tintColor The tint color.
  82. @return The new image with the tint color.
  83. */
  84. - (nullable UIImage *)sd_tintedImageWithColor:(nonnull UIColor *)tintColor;
  85. /**
  86. Return the pixel color at specify position. The point is from the top-left to the bottom-right and 0-based. The returned the color is always be RGBA format. The image must be CG-based.
  87. @note The point's x/y should not be smaller than 0, or greater than or equal to width/height.
  88. @note The overhead of object creation means this method is best suited for infrequent color sampling. For heavy image processing, grab the raw bitmap data and process yourself.
  89. @param point The position of pixel
  90. @return The color for specify pixel, or nil if any error occur
  91. */
  92. - (nullable UIColor *)sd_colorAtPoint:(CGPoint)point;
  93. /**
  94. Return the pixel color array with specify rectangle. The rect is from the top-left to the bottom-right and 0-based. The returned the color is always be RGBA format. The image must be CG-based.
  95. @note The rect's width/height should not be smaller than or equal to 0. The minX/minY should not be smaller than 0. The maxX/maxY should not be greater than width/height. Attention this limit is different from `sd_colorAtPoint:` (point: (0, 0) like rect: (0, 0, 1, 1))
  96. @note The overhead of object creation means this method is best suited for infrequent color sampling. For heavy image processing, grab the raw bitmap data and process yourself.
  97. @param rect The rectangle of pixels
  98. @return The color array for specify pixels, or nil if any error occur
  99. */
  100. - (nullable NSArray<UIColor *> *)sd_colorsWithRect:(CGRect)rect;
  101. #pragma mark - Image Effect
  102. /**
  103. Return a new image applied a blur effect.
  104. @param blurRadius The radius of the blur in points, 0 means no blur effect.
  105. @return The new image with blur effect, or nil if an error occurs (e.g. no enough memory).
  106. */
  107. - (nullable UIImage *)sd_blurredImageWithRadius:(CGFloat)blurRadius;
  108. #if SD_UIKIT || SD_MAC
  109. /**
  110. Return a new image applied a CIFilter.
  111. @param filter The CIFilter to be applied to the image.
  112. @return The new image with the CIFilter, or nil if an error occurs (e.g. no
  113. enough memory).
  114. */
  115. - (nullable UIImage *)sd_filteredImageWithFilter:(nonnull CIFilter *)filter;
  116. #endif
  117. @end