Saik0s
2/20/2017 - 11:59 AM

UIImage+Tint+Resize.swift

import UIKit

extension UIImage {
    func tinted(withLinearGradient gradient: CGGradient, fromTop: Bool = true, blendMode: CGBlendMode = CGBlendMode.normal) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale);
        let context = UIGraphicsGetCurrentContext()!
        context.translateBy(x: 0, y: self.size.height)
        context.scaleBy(x: 1.0, y: -1.0)

        context.setBlendMode(blendMode)
        let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)

        let startPoint: CGPoint = CGPoint(x: 0, y: 0)
        var endPoint: CGPoint = CGPoint(x: 0, y: self.size.height)
        if !fromTop {
            endPoint = CGPoint(x: self.size.width, y: 0)
        }
        context.clip(to: rect, mask: self.cgImage!)
        context.drawLinearGradient(gradient, start: startPoint, end: endPoint, options: CGGradientDrawingOptions(rawValue: 0))
        let gradientImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return gradientImage!
    }

    func tinted(withLinearGradientColors colorsArr: [UIColor], locations: [CGFloat], fromTop: Bool = true, blendMode: CGBlendMode = CGBlendMode.normal) -> UIImage {
        guard colorsArr.count > 1 || colorsArr.count == locations.count else {
            return self
        }
        let cgColorsArr: [CGColor] = colorsArr.map { (color) -> CGColor in
            return color.cgColor
        }
        let gradient: CGGradient = CGGradient(colorsSpace: nil, colors: cgColorsArr as CFArray, locations: locations)!
        return self.tinted(withLinearGradient: gradient, fromTop: fromTop, blendMode: blendMode)
    }

    func tinted(withColor color: UIColor) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(self.size, false, UIScreen.main.scale)

        let context = UIGraphicsGetCurrentContext()
        color.setFill()

        context!.translateBy(x: 0, y: self.size.height)
        context!.scaleBy(x: 1.0, y: -1.0)

        context!.setBlendMode(CGBlendMode.colorBurn)
        let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
        context?.draw(self.cgImage!, in: rect)

        context!.setBlendMode(CGBlendMode.sourceIn)
        context!.addRect(rect)
        context!.drawPath(using: CGPathDrawingMode.fill)

        let coloredImg : UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        
        return coloredImg
    }

    func resizeWithCurrentRatio(toSize targetSize: CGSize) -> UIImage {
        let widthRatio  = targetSize.width  / self.size.width
        let heightRatio = targetSize.height / self.size.height

        var newSize: CGSize
        if(widthRatio > heightRatio) {
            newSize = CGSize(width: self.size.width * heightRatio, height: self.size.height * heightRatio)
        } else {
            newSize = CGSize(width: self.size.width * widthRatio, height: self.size.height * widthRatio)
        }

        let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)

        UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
        self.draw(in: rect)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        
        return newImage
    }
}