Last active
August 22, 2017 10:36
-
-
Save yukitoto/e8aa420e20be7ab5d64b71e96ecd61f1 to your computer and use it in GitHub Desktop.
Extension For change UIImage. This is useful for set highlighted image for UIButton.
This file contains hidden or 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
// | |
// UIImage+Extension.swift | |
// | |
// Created by Yukito Shibuya on 2016/10/08. | |
// Copyright © 2016年 yukitoto. All rights reserved. | |
// | |
import UIKit | |
extension UIImage { | |
func imageWithAlphaComponent(alpha: CGFloat) -> UIImage { | |
UIGraphicsBeginImageContextWithOptions(self.size, false, 0) | |
let ctx = UIGraphicsGetCurrentContext() | |
let area = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height) | |
CGContextScaleCTM(ctx, 1, -1) | |
CGContextTranslateCTM(ctx, 0, -area.size.height) | |
CGContextSetBlendMode(ctx, CGBlendMode.Multiply) | |
CGContextSetAlpha(ctx, alpha) | |
CGContextDrawImage(ctx, area, self.CGImage) | |
let newImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return newImage | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is amazing!!! Thank you 👍