Skip to content

Instantly share code, notes, and snippets.

@dclelland
Created September 17, 2017 11:48
Show Gist options
  • Save dclelland/1391bae781182a323caf4a1a1e7774dd to your computer and use it in GitHub Desktop.
Save dclelland/1391bae781182a323caf4a1a1e7774dd to your computer and use it in GitHub Desktop.
Testing out the new UIGraphicsImageRenderer class on iOS 11
import UIKit
extension UIImage {
convenience init?(color: UIColor, size: CGSize = CGSize(width: 1.0, height: 1.0)) {
let image = UIGraphicsImageRenderer(size: size).image { context in
color.setFill()
context.fill(CGRect(origin: .zero, size: size))
}
guard let cgImage = image.cgImage else {
return nil
}
self.init(cgImage: cgImage)
}
}
extension UIImage {
func tinted(_ color: UIColor) -> UIImage? {
return UIGraphicsImageRenderer(size: size).image { context in
color.setFill()
context.fill(context.format.bounds)
draw(in: context.format.bounds, blendMode: .destinationIn, alpha: 1.0)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment