UIImage animationImages色彩?

问题描述 投票:9回答:5

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

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

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

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

谢谢

ios xcode swift uiimage
5个回答
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;
}

然后你可以这样称呼它:

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

4
投票

这是一个方便的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;
    }

}

4
投票

关于这个问题(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!
    }
}

0
投票

这是Swift 4的更新代码,带有一些安全检查。

extension UIImage {
    func image(withTint tint: UIColor) -> UIImage? {
        guard let cgImage = cgImage else {
            return nil
        }

        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        guard let context = UIGraphicsGetCurrentContext() else {
            return nil
        }

        let rect = CGRect(origin: .zero, size: size)

        context.translateBy(x: 0, y: size.height)
        context.scaleBy(x: 1.0, y: -1.0)
        context.setBlendMode(.normal)
        context.clip(to: rect, mask: cgImage)
        tint.setFill()
        context.fill(rect)

        let image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
    }
}

0
投票

也许这个示例扩展有助于:

 extension UIImageView {

        func pulsingTintColor() {
            UIView.animate(withDuration: 2, delay: 0.0, options: [.repeat, .autoreverse], animations: {
                         self.tintColor = UIColor.red
                         self.tintColor = UIColor.green
                         self.tintColor = UIColor.blue
            }, completion: nil)
        }
    }

确保已将“渲染为:模板图像”选项设置在资产目录中。这也适用于UIViews。只需用backgroundColor替换tintColor即可。

如果您需要参数化颜色:

func pulsingTintColor(with colors: [UIColor] = [UIColor.red, UIColor.green, UIColor.blue]) {
    UIView.animate(withDuration: 2, delay: 0.0, options: [.repeat, .autoreverse], animations: {
         colors.forEach({self.tintColor = $0})
    }, completion: nil)
}
© www.soinside.com 2019 - 2024. All rights reserved.