swift 的UIImage +色调+ Resize.swift

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 的UIImage +色调+ 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
    }
}

以上是关于swift 的UIImage +色调+ Resize.swift的主要内容,如果未能解决你的问题,请参考以下文章

设置 UIImage 颜色

Swift:更改标签栏的图像色调颜色?

XIB中UIImage的色调颜色

我可以更改 UIButton 上的色调颜色,但不能更改 UIImage

如何只在几个部分中以编程方式更改UIImage的色调?

色调颜色正在改变导航栏中图像的颜色 swift 3