Created
July 15, 2015 14:01
-
-
Save vknabel/1f50adbab00fe0e3f5c1 to your computer and use it in GitHub Desktop.
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 | |
public extension UIImage { | |
public func imageWithColor(color: UIColor) -> UIImage { | |
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale) | |
let context: CGContextRef = UIGraphicsGetCurrentContext() | |
CGContextTranslateCTM(context, 0, self.size.height) | |
CGContextScaleCTM(context, 1.0, -1.0) | |
CGContextSetBlendMode(context, CGBlendMode.Normal) | |
let rect: CGRect = CGRectMake(0, 0, self.size.width, self.size.height) | |
CGContextClipToMask(context, rect, self.CGImage) | |
color.setFill() | |
CGContextFillRect(context, rect) | |
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return newImage | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment