42 lines
1.1 KiB
Swift
42 lines
1.1 KiB
Swift
//
|
|
// gp_pboto.swift
|
|
// Hthik
|
|
//
|
|
// Created by 忆海16 on 2024/5/31.
|
|
// Copyright © 2024 Hthik. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
extension UIImage {
|
|
|
|
func withTintColor(_ color: UIColor) -> UIImage? {
|
|
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
|
|
defer { UIGraphicsEndImageContext() }
|
|
|
|
guard let context = UIGraphicsGetCurrentContext(), let cgImage = self.cgImage else {
|
|
return nil
|
|
}
|
|
|
|
let rect = CGRect(origin: .zero, size: self.size)
|
|
|
|
// Flip the context vertically
|
|
context.translateBy(x: 0, y: self.size.height)
|
|
context.scaleBy(x: 1.0, y: -1.0)
|
|
|
|
// Draw the original image
|
|
context.draw(cgImage, in: rect)
|
|
|
|
// Apply the color overlay
|
|
context.setBlendMode(.sourceIn)
|
|
color.setFill()
|
|
context.fill(rect)
|
|
|
|
// Create a new UIImage from the context
|
|
let tintedImage = UIGraphicsGetImageFromCurrentImageContext()
|
|
|
|
return tintedImage
|
|
}
|
|
}
|