Skip to content

Instantly share code, notes, and snippets.

@vknabel
Created July 15, 2015 14:01
Show Gist options
  • Save vknabel/1f50adbab00fe0e3f5c1 to your computer and use it in GitHub Desktop.
Save vknabel/1f50adbab00fe0e3f5c1 to your computer and use it in GitHub Desktop.
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