UIImage animationImages couleur de la teinte?

Est-il possible de colorer les images dans une animation?

Je sais que je peux teinter une seule image comme ceci:

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

Mais quand j'essaye de le faire comme ça, ça ne marche pas:

  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(); 

Est-ce que je fais quelque chose de mal? Y at-il un moyen de colorer les images dans l'animation?

Merci

Ok, j'espérais qu'il existe une solution plus simple mais c'est ce que j'ai fini par faire:

Cette fonction va créer une nouvelle image avec la couleur voulue:

 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; } 

Et puis vous pouvez l'appeler comme ceci:

 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(); 

Voici une extension UIImage pratique:

 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; } }