Files
yingmeng-ios-switf/yinmeng-ios/Extension/UIImage/UIImage+.swift

130 lines
4.9 KiB
Swift
Raw Normal View History

2024-02-21 21:30:13 +08:00
//
// UIImage+.swift
// yinmeng-ios
//
2024-03-05 14:04:09 +08:00
// Created by yinmeng on 2024/2/21.
2024-02-21 21:30:13 +08:00
//
import Foundation
// MARK:- UIColor
extension UIImage{
// MARK: 2.1
///
/// - Parameters:
/// - color:
/// - size:
/// - Returns:
static func image(color: UIColor, size: CGSize = CGSize(width: 1.0, height: 1.0)) -> UIImage? {
return image(color: color, size: size, corners: .allCorners, radius: 0)
}
// MARK: 2.2
///
/// - Parameters:
/// - color:
/// - size:
/// - corners:
/// - round:
/// - Returns:
static func image(color: UIColor, size: CGSize, corners: UIRectCorner, radius: CGFloat) -> UIImage? {
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
let context = UIGraphicsGetCurrentContext()
if radius > 0 {
let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
color.setFill()
path.fill()
} else {
context?.setFillColor(color.cgColor)
context?.fill(rect)
}
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
enum GradientDirection {
case horizontal //
case vertical //
case leftOblique //
case rightOblique //
case other(CGPoint, CGPoint)
public func point(size: CGSize) -> (CGPoint, CGPoint) {
switch self {
case .horizontal:
return (CGPoint.init(x: 0, y: 0), CGPoint.init(x: size.width, y: 0))
case .vertical:
return (CGPoint.init(x: 0, y: 0), CGPoint.init(x: 0, y: size.height))
case .leftOblique:
return (CGPoint.init(x: 0, y: 0), CGPoint.init(x: size.width, y: size.height))
case .rightOblique:
return (CGPoint.init(x: size.width, y: 0), CGPoint.init(x: 0, y: size.height))
case .other(let stat, let end):
return (stat, end)
}
}
}
// MARK: 2.3 ["#B0E0E6", "#00CED1", "#2E8B57"]
/// ["#B0E0E6", "#00CED1", "#2E8B57"]
/// - Parameters:
/// - hexsString:
/// - size:
/// - locations: locations
/// - direction:
/// - Returns:
static func gradient(hexsString: [String], size: CGSize = CGSize(width: 10, height: 10), locations:[CGFloat]? = nil, direction: GradientDirection = .horizontal) -> UIImage? {
return gradient(hexsString.map{ ThemeColor(hexStr: $0) }, size: size, locations: locations, direction: direction)
}
// MARK: 2.4 [UIColor, UIColor, UIColor]
/// [UIColor, UIColor, UIColor]
/// - Parameters:
/// - colors: UIColor
/// - size:
/// - locations: locations
/// - direction:
/// - Returns:
static func gradient(_ colors: [UIColor], size: CGSize = CGSize(width: 10, height: 10), locations:[CGFloat]? = nil, direction: GradientDirection = .horizontal) -> UIImage? {
return gradient(colors, size: size, radius: 0, locations: locations, direction: direction)
}
// MARK: 2.5 [UIColor, UIColor, UIColor]
/// [UIColor, UIColor, UIColor]
/// - Parameters:
/// - colors: UIColor
/// - size:
/// - radius:
/// - locations: locations
/// - direction:
/// - Returns:
static func gradient(_ colors: [UIColor],
size: CGSize = CGSize(width: 10, height: 10),
radius: CGFloat,
locations:[CGFloat]? = nil,
direction: GradientDirection = .horizontal) -> UIImage? {
if colors.count == 0 { return nil }
if colors.count == 1 {
return UIImage.image(color: colors[0])
}
UIGraphicsBeginImageContext(size)
let context = UIGraphicsGetCurrentContext()
let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: size.width, height: size.height), cornerRadius: radius)
path.addClip()
context?.addPath(path.cgPath)
guard let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors.map{$0.cgColor} as CFArray, locations: locations?.map { CGFloat($0) }) else { return nil
}
let directionPoint = direction.point(size: size)
context?.drawLinearGradient(gradient, start: directionPoint.0, end: directionPoint.1, options: .drawsBeforeStartLocation)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
2024-03-07 16:56:48 +08:00
extension UIImage{
static func getDefaultAvatar() -> UIImage?{
return UIImage(named: "yin_default_avatar") ?? nil
}
}