UIImage+Extension.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // UIImage+Extension.swift
  3. // AiKeyboard
  4. //
  5. // Created by Destiny on 2025/4/25.
  6. //
  7. import Foundation
  8. import UIKit
  9. public enum ImageGradientDirection {
  10. /// 水平从左到右
  11. case horizontal
  12. /// 垂直从上到下
  13. case vertical
  14. /// 左上到右下
  15. case leftOblique
  16. /// 右上到左下
  17. case rightOblique
  18. /// 自定义
  19. case other(CGPoint, CGPoint)
  20. public func point(size: CGSize) -> (CGPoint, CGPoint) {
  21. switch self {
  22. case .horizontal:
  23. return (CGPoint(x: 0, y: 0), CGPoint(x: size.width, y: 0))
  24. case .vertical:
  25. return (CGPoint(x: 0, y: 0), CGPoint(x: 0, y: size.height))
  26. case .leftOblique:
  27. return (CGPoint(x: 0, y: 0), CGPoint(x: size.width, y: size.height))
  28. case .rightOblique:
  29. return (CGPoint(x: size.width, y: 0), CGPoint(x: 0, y: size.height))
  30. case .other(let stat, let end):
  31. return (stat, end)
  32. }
  33. }
  34. }
  35. extension UIImage {
  36. static func image(color: UIColor, size: CGSize = CGSize(width: 1.0, height: 1.0)) -> UIImage? {
  37. return image(color: color, size: size, corners: .allCorners, radius: 0)
  38. }
  39. static func image(color: UIColor, size: CGSize, corners: UIRectCorner, radius: CGFloat) -> UIImage? {
  40. let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
  41. // 防止size:(0, 0)崩溃
  42. var drawSize = size
  43. if drawSize.width <= 0 || drawSize.height <= 0 {
  44. drawSize = CGSize(width: 1, height: 1)
  45. }
  46. UIGraphicsBeginImageContextWithOptions(drawSize, false, UIScreen.main.scale)
  47. let context = UIGraphicsGetCurrentContext()
  48. if radius > 0 {
  49. let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
  50. color.setFill()
  51. path.fill()
  52. } else {
  53. context?.setFillColor(color.cgColor)
  54. context?.fill(rect)
  55. }
  56. let img = UIGraphicsGetImageFromCurrentImageContext()
  57. UIGraphicsEndImageContext()
  58. return img
  59. }
  60. static func gradient(_ colors: [UIColor], size: CGSize = CGSize(width: 10, height: 10), locations:[CGFloat]? = nil, direction: ImageGradientDirection = .horizontal) -> UIImage? {
  61. return gradient(colors, size: size, radius: 0, locations: locations, direction: direction)
  62. }
  63. // MARK: 2.5、生成带圆角渐变色的图片 [UIColor, UIColor, UIColor]
  64. /// 生成带圆角渐变色的图片 [UIColor, UIColor, UIColor]
  65. /// - Parameters:
  66. /// - colors: UIColor 数组
  67. /// - size: 图片大小
  68. /// - radius: 圆角
  69. /// - locations: locations 数组
  70. /// - direction: 渐变的方向
  71. /// - Returns: 带圆角的渐变的图片
  72. static func gradient(_ colors: [UIColor],
  73. size: CGSize = CGSize(width: 10, height: 10),
  74. radius: CGFloat,
  75. locations:[CGFloat]? = nil,
  76. direction: ImageGradientDirection = .horizontal) -> UIImage? {
  77. if colors.count == 0 { return nil }
  78. if colors.count == 1 {
  79. return image(color: colors[0])
  80. }
  81. UIGraphicsBeginImageContext(size)
  82. let context = UIGraphicsGetCurrentContext()
  83. let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: size.width, height: size.height), cornerRadius: radius)
  84. path.addClip()
  85. context?.addPath(path.cgPath)
  86. guard let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors.map{$0.cgColor} as CFArray, locations: locations?.map { CGFloat($0) }) else { return nil
  87. }
  88. let directionPoint = direction.point(size: size)
  89. context?.drawLinearGradient(gradient, start: directionPoint.0, end: directionPoint.1, options: .drawsBeforeStartLocation)
  90. let image = UIGraphicsGetImageFromCurrentImageContext()
  91. UIGraphicsEndImageContext()
  92. return image
  93. }
  94. }