Skip to content

Instantly share code, notes, and snippets.

@Busta117
Last active October 3, 2016 19:16
Show Gist options
  • Save Busta117/a1c433af333e3d13c451423d195f6d57 to your computer and use it in GitHub Desktop.
Save Busta117/a1c433af333e3d13c451423d195f6d57 to your computer and use it in GitHub Desktop.
extension UIImage tint color swift 3
public extension UIView {
/**
Convert current view to image
:returns: return the image
*/
public func convertToImage() -> UIImage {
UIGraphicsBeginImageContext(self.bounds.size);
self.layer.render(in: UIGraphicsGetCurrentContext()!)
let viewImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return viewImage!
}
}
public extension UIImage {
public class func `init`(fromColor color: UIColor, size: CGSize = CGSize(width: 10, height: 10)) -> UIImage{
let imageView = UIView(frame: CGRect(x:0, y:0, width:size.width, height:size.height))
imageView.backgroundColor = color
return imageView.convertToImage()
}
public func maskImage(mask:UIImage!)->UIImage{
let imageReference = self.cgImage
let maskReference = mask.cgImage!
let imageMask = CGImage(maskWidth: maskReference.width,
height: maskReference.height,
bitsPerComponent: maskReference.bitsPerComponent,
bitsPerPixel: maskReference.bitsPerPixel,
bytesPerRow: maskReference.bytesPerRow,
provider: maskReference.dataProvider!, decode: nil, shouldInterpolate: true)
let maskedReference = imageReference!.masking(imageMask!)
let maskedImage = UIImage(cgImage:maskedReference!)
return maskedImage
}
public func image(withTintColor color: UIColor) -> UIImage{
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
let context: CGContext = UIGraphicsGetCurrentContext()!
context.translateBy(x: 0, y: self.size.height)
context.scaleBy(x: 1.0, y: -1.0)
context.setBlendMode(CGBlendMode.normal)
let rect: CGRect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
context.clip(to: rect, mask: self.cgImage!)
color.setFill()
context.fill(rect);
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!;
UIGraphicsEndImageContext();
return newImage;
}
public class func `init`(named name:String, tintColor: UIColor) -> UIImage{
let image: UIImage = UIImage(named: name)!
return image.image(withTintColor: tintColor)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment