Revision control
Copy as Markdown
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
import UIKit
extension UIImage {
/// The image's shape filled with a top to bottom gradient of `stops`.
func verticalGradientTinted(with stops: [UIColor]) -> UIImage? {
guard let gradient = CGGradient(
colorsSpace: CGColorSpaceCreateDeviceRGB(),
colors: stops.map(\.cgColor) as CFArray,
locations: nil
) else { return nil }
let rect = CGRect(origin: .zero, size: size)
return UIGraphicsImageRenderer(size: size).image { context in
context.cgContext.drawLinearGradient(
gradient,
start: CGPoint(x: rect.midX, y: rect.minY),
end: CGPoint(x: rect.midX, y: rect.maxY),
options: []
)
draw(in: rect, blendMode: .destinationIn, alpha: 1.0)
}.withRenderingMode(.alwaysOriginal)
}
}