UIImage animationImages色调?

有没有办法在animation中对图像进行着色?

我知道我可以像这样一个单一的图像:

var imageOne:UIImage = UIImage(named: "pullto_1.png")!; imageOne = imageOne.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate); refSequence.image = imageOne; 

但是当我试图这样做的时候,它就不能工作:

  var imageOne:UIImage = UIImage(named: "pullto_1.png")!; imageOne = imageOne.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) var image2:UIImage = UIImage(named: "pullto_2.png")!; image2 = image2.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) var image3:UIImage = UIImage(named: "pullto_3.png")!; image3 = image3.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) var image4:UIImage = UIImage(named: "pullto_4.png")!; image4 = image4.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) refSequence.animationImages = NSArray(objects: imageOne, image2, image3, image4 ); refSequence.animationDuration = 1.4; refSequence.animationRepeatCount = 99; refSequence.startAnimating(); 

难道我做错了什么? 有什么方法来animation中的图像色调?

谢谢

好吧,我希望有一个更简单的解决scheme,但这是我最终做的:

此function将创build一个新的图像与所需的颜色:

 func imageWithColor(img:UIImage, color:UIColor)->UIImage{ UIGraphicsBeginImageContextWithOptions(img.size, false, img.scale); var context = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(context, 0, img.size.height); CGContextScaleCTM(context, 1.0, -1.0); CGContextSetBlendMode(context, kCGBlendModeNormal); var rect = CGRectMake(0, 0, img.size.width, img.size.height); CGContextClipToMask(context, rect, img.CGImage) color.setFill(); CGContextFillRect(context, rect); var newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 

然后你可以这样称呼它:

 var imageOne:UIImage = UIImage(named: "pullto_1.png")!; imageOne = imageWithColor(imageOne, color: UIColor.redColor()); var image2:UIImage = UIImage(named: "pullto_2.png")!; image2 = imageWithColor(image2, color: UIColor.redColor()); var image3:UIImage = UIImage(named: "pullto_3.png")!; image3 = imageWithColor(image3, color: UIColor.redColor()); var image4:UIImage = UIImage(named: "pullto_4.png")!; image4 = imageWithColor(image4, color: UIColor.redColor()); loaderS.animationImages = NSArray(objects: imageOne, image2, image3, image4 ); loaderS.animationDuration = 1.4; loaderS.animationRepeatCount = 99; loaderS.startAnimating(); 

这是一个方便的UIImage扩展:

 import UIKit extension UIImage { func imageWithTint(tint: UIColor) -> UIImage { UIGraphicsBeginImageContextWithOptions(size, false, scale) let context = UIGraphicsGetCurrentContext() CGContextTranslateCTM(context, 0, size.height) CGContextScaleCTM(context, 1.0, -1.0) CGContextSetBlendMode(context, .Normal) let rect = CGRect(origin: .zero, size: size) CGContextClipToMask(context, rect, CGImage) tint.setFill() CGContextFillRect(context, rect) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image; } }