ios中UIImage中改色的问题

改色有很多细节,我们这里只探讨最简单的,全部有色点改色

var newImage = image.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
UIGraphicsBeginImageContextWithOptions(newImage.size, false, newImage.scale);
UIColor.redColor().set()
newImage.drawInRect(CGRectMake(0, 0, newImage.size.width, newImage.size.height))
newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

当然,你可以可以写一个Extension

extension UIImage {
    func imageWithTintColor(color: UIColor) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        color.set()
        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        UIRectFill(rect)
        drawInRect(rect, blendMode: CGBlendMode.DestinationIn, alpha: 1.0)
        let tintedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return tintedImage
    }
}

更多细节可以参考猫神的文章:https://onevcat.com/2013/04/using-blending-in-ios/

 

Leave a Reply

Your email address will not be published. Required fields are marked *