UIImage+Extension.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // UIImage+Extension.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by mac on 2024/4/10.
  6. //
  7. import UIKit
  8. public enum ImageGradientDirection {
  9. /// 水平从左到右
  10. case horizontal
  11. /// 垂直从上到下
  12. case vertical
  13. /// 左上到右下
  14. case leftOblique
  15. /// 右上到左下
  16. case rightOblique
  17. /// 自定义
  18. case other(CGPoint, CGPoint)
  19. public func point(size: CGSize) -> (CGPoint, CGPoint) {
  20. switch self {
  21. case .horizontal:
  22. return (CGPoint(x: 0, y: 0), CGPoint(x: size.width, y: 0))
  23. case .vertical:
  24. return (CGPoint(x: 0, y: 0), CGPoint(x: 0, y: size.height))
  25. case .leftOblique:
  26. return (CGPoint(x: 0, y: 0), CGPoint(x: size.width, y: size.height))
  27. case .rightOblique:
  28. return (CGPoint(x: size.width, y: 0), CGPoint(x: 0, y: size.height))
  29. case .other(let stat, let end):
  30. return (stat, end)
  31. }
  32. }
  33. }
  34. extension UIImage {
  35. // MARK: 2.1、生成指定尺寸的纯色图像
  36. /// 生成指定尺寸的纯色图像
  37. /// - Parameters:
  38. /// - color: 图片颜色
  39. /// - size: 图片尺寸
  40. /// - Returns: 返回对应的图片
  41. static func image(color: UIColor, size: CGSize = CGSize(width: 1.0, height: 1.0)) -> UIImage? {
  42. return image(color: color, size: size, corners: .allCorners, radius: 0)
  43. }
  44. // MARK: 2.2、生成指定尺寸和圆角的纯色图像
  45. /// 生成指定尺寸和圆角的纯色图像
  46. /// - Parameters:
  47. /// - color: 图片颜色
  48. /// - size: 图片尺寸
  49. /// - corners: 剪切的方式
  50. /// - round: 圆角大小
  51. /// - Returns: 返回对应的图片
  52. static func image(color: UIColor, size: CGSize, corners: UIRectCorner, radius: CGFloat) -> UIImage? {
  53. let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
  54. // 防止size:(0, 0)崩溃
  55. var drawSize = size
  56. if drawSize.width <= 0 || drawSize.height <= 0 {
  57. drawSize = CGSize(width: 1, height: 1)
  58. }
  59. UIGraphicsBeginImageContextWithOptions(drawSize, false, UIScreen.main.scale)
  60. let context = UIGraphicsGetCurrentContext()
  61. if radius > 0 {
  62. let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
  63. color.setFill()
  64. path.fill()
  65. } else {
  66. context?.setFillColor(color.cgColor)
  67. context?.fill(rect)
  68. }
  69. let img = UIGraphicsGetImageFromCurrentImageContext()
  70. UIGraphicsEndImageContext()
  71. return img
  72. }
  73. // MARK: 2.3、生成渐变色的图片 ["#B0E0E6", "#00CED1", "#2E8B57"]
  74. /// 生成渐变色的图片 ["#B0E0E6", "#00CED1", "#2E8B57"]
  75. /// - Parameters:
  76. /// - hexsString: 十六进制字符数组
  77. /// - size: 图片大小
  78. /// - locations: locations 数组
  79. /// - direction: 渐变的方向
  80. /// - Returns: 渐变的图片
  81. static func gradient(_ hexsString: [String], size: CGSize = CGSize(width: 1, height: 1), locations:[CGFloat]? = nil, direction: ImageGradientDirection = .horizontal) -> UIImage? {
  82. return gradient(hexsString.map{ UIColor.hexStringColor(hexString: $0) }, size: size, locations: locations, direction: direction)
  83. }
  84. // MARK: 2.4、生成渐变色的图片 [UIColor, UIColor, UIColor]
  85. /// 生成渐变色的图片 [UIColor, UIColor, UIColor]
  86. /// - Parameters:
  87. /// - colors: UIColor 数组
  88. /// - size: 图片大小
  89. /// - locations: locations 数组
  90. /// - direction: 渐变的方向
  91. /// - Returns: 渐变的图片
  92. static func gradient(_ colors: [UIColor], size: CGSize = CGSize(width: 10, height: 10), locations:[CGFloat]? = nil, direction: ImageGradientDirection = .horizontal) -> UIImage? {
  93. return gradient(colors, size: size, radius: 0, locations: locations, direction: direction)
  94. }
  95. // MARK: 2.5、生成带圆角渐变色的图片 [UIColor, UIColor, UIColor]
  96. /// 生成带圆角渐变色的图片 [UIColor, UIColor, UIColor]
  97. /// - Parameters:
  98. /// - colors: UIColor 数组
  99. /// - size: 图片大小
  100. /// - radius: 圆角
  101. /// - locations: locations 数组
  102. /// - direction: 渐变的方向
  103. /// - Returns: 带圆角的渐变的图片
  104. static func gradient(_ colors: [UIColor],
  105. size: CGSize = CGSize(width: 10, height: 10),
  106. radius: CGFloat,
  107. locations:[CGFloat]? = nil,
  108. direction: ImageGradientDirection = .horizontal) -> UIImage? {
  109. if colors.count == 0 { return nil }
  110. if colors.count == 1 {
  111. return image(color: colors[0])
  112. }
  113. UIGraphicsBeginImageContext(size)
  114. let context = UIGraphicsGetCurrentContext()
  115. let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: size.width, height: size.height), cornerRadius: radius)
  116. path.addClip()
  117. context?.addPath(path.cgPath)
  118. guard let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors.map{$0.cgColor} as CFArray, locations: locations?.map { CGFloat($0) }) else { return nil
  119. }
  120. let directionPoint = direction.point(size: size)
  121. context?.drawLinearGradient(gradient, start: directionPoint.0, end: directionPoint.1, options: .drawsBeforeStartLocation)
  122. let image = UIGraphicsGetImageFromCurrentImageContext()
  123. UIGraphicsEndImageContext()
  124. return image
  125. }
  126. }