Created
September 17, 2017 11:48
-
-
Save dclelland/1391bae781182a323caf4a1a1e7774dd to your computer and use it in GitHub Desktop.
Testing out the new UIGraphicsImageRenderer class on iOS 11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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