Skip to content

Instantly share code, notes, and snippets.

@yukitoto
Last active August 22, 2017 10:36
Show Gist options
  • Save yukitoto/e8aa420e20be7ab5d64b71e96ecd61f1 to your computer and use it in GitHub Desktop.
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.
//
// 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
}
}
@4l1k
Copy link

4l1k commented Aug 22, 2017

This is amazing!!! Thank you 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment