UIImage animationImages色彩?

Fer*_*enc 9 xcode uiimage ios swift

有没有办法在动画中着色图像?

我知道我可以像这样着色一个图像:

var imageOne:UIImage = UIImage(named: "pullto_1.png")!;
    imageOne = imageOne.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
    refSequence.image = imageOne;
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试这样做时,它只是不起作用:

 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();
Run Code Online (Sandbox Code Playgroud)

难道我做错了什么?有没有办法在动画中着色图像?

谢谢

Fer*_*enc 10

好的,我希望有一个更简单的解决方案,但这就是我最终做的事情:

此功能将创建一个具有所需颜色的新图像:

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;
}
Run Code Online (Sandbox Code Playgroud)

然后你可以这样称呼它:

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();
Run Code Online (Sandbox Code Playgroud)


emr*_*kyv 5

有一个关于此问题的 rdar ( http://www.openradar.me/23517334 ),并且该问题在 iOS 11 上仍然存在。

我将上面的代码示例改编为 Swift 4。

extension UIImage {
    func image(withTintColor color: UIColor) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size, false, scale)

        let context = UIGraphicsGetCurrentContext()
        context?.translateBy(x: 0, y: size.height)
        context?.scaleBy(x: 1.0, y: -1.0)
        context?.setBlendMode(.normal)

        let rect = CGRect(origin: .zero, size: size)
        context?.clip(to: rect, mask: cgImage!)
        color.setFill()
        context?.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return image!
    }
}
Run Code Online (Sandbox Code Playgroud)