1、扩展Image
extension UIImage {
func imageWithColor(tintColor: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
let context = UIGraphicsGetCurrentContext() as CGContextRef
CGContextTranslateCTM(context, 0, self.size.height)
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, kCGBlendModeNormal)
let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect
CGContextClipToMask(context, rect, self.CGImage)
tintColor.setFill()
CGContextFillRect(context, rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
UIGraphicsEndImageContext()
return newImage
}
} 2、使用
clockImage.image = clockImage.image!.imageWithColor(color).imageWithRenderingMode(.AlwaysOriginal)

本文介绍了如何在iOS应用中利用Swift编程语言扩展UIImage类,实现动态改变图片颜色的功能。通过自定义扩展,可以方便地为图片应用不同色彩效果。
1166

被折叠的 条评论
为什么被折叠?



