180 lines
7.0 KiB
Swift
180 lines
7.0 KiB
Swift
//
|
||
// UIImage+.swift
|
||
// yinmeng-ios
|
||
//
|
||
// Created by yinmeng on 2024/2/21.
|
||
//
|
||
|
||
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
|
||
}
|
||
|
||
|
||
|
||
static func createImageWithCorners(color:UIColor, size: CGSize, topLeft: CGFloat, topRight: CGFloat, bottomLeft: CGFloat, bottomRight: CGFloat) -> UIImage? {
|
||
UIGraphicsBeginImageContextWithOptions(size, false, 0)
|
||
guard let context = UIGraphicsGetCurrentContext() else { return nil }
|
||
|
||
let path = UIBezierPath(rect: CGRect(origin: .zero, size: size))
|
||
context.addPath(path.cgPath)
|
||
|
||
// 设置左上角的圆角
|
||
path.move(to: CGPoint(x: topLeft, y: 0))
|
||
path.addLine(to: CGPoint(x: 0, y: topLeft))
|
||
if topLeft > 0 {
|
||
path.addArc(withCenter: CGPoint(x: topLeft, y: topLeft), radius: topLeft, startAngle: CGFloat.pi, endAngle: CGFloat.pi * 3 / 2, clockwise: true)
|
||
}
|
||
|
||
// 设置右上角的圆角
|
||
let topRightFrom = CGPoint(x: size.width - topRight, y: 0)
|
||
path.addLine(to: topRightFrom)
|
||
if topRight > 0 {
|
||
path.addArc(withCenter: CGPoint(x: size.width - topRight, y: topRight), radius: topRight, startAngle: CGFloat.pi * 3 / 2, endAngle: CGFloat.pi, clockwise: true)
|
||
}
|
||
|
||
// 设置左下角的圆角
|
||
let bottomLeftFrom = CGPoint(x: 0, y: size.height - bottomLeft)
|
||
path.addLine(to: bottomLeftFrom)
|
||
if bottomLeft > 0 {
|
||
path.addArc(withCenter: CGPoint(x: bottomLeft, y: size.height - bottomLeft), radius: bottomLeft, startAngle: 0, endAngle: CGFloat.pi / 2, clockwise: false)
|
||
}
|
||
|
||
// 设置右下角的圆角
|
||
path.addLine(to: CGPoint(x: size.width, y: size.height - bottomRight))
|
||
if bottomRight > 0 {
|
||
path.addArc(withCenter: CGPoint(x: size.width - bottomRight, y: size.height - bottomRight), radius: bottomRight, startAngle: CGFloat.pi / 2, endAngle: 0, clockwise: false)
|
||
}
|
||
|
||
path.close()
|
||
context.clip()
|
||
|
||
// 填充颜色,或者可以在这里添加自定义的图片
|
||
context.setFillColor(color.cgColor)
|
||
context.fillPath()
|
||
|
||
let image = UIGraphicsGetImageFromCurrentImageContext()
|
||
UIGraphicsEndImageContext()
|
||
return image
|
||
}
|
||
|
||
|
||
}
|
||
|
||
extension UIImage{
|
||
static func getDefaultAvatar() -> UIImage?{
|
||
return UIImage(named: "yin_default_avatar") ?? nil
|
||
}
|
||
}
|