UIImage animationImages色彩?

时间:2014-11-26 12:16:59

标签: ios xcode swift uiimage

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

我知道我可以为这样的单个图像着色:

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

我做错了吗?有没有办法在动画中着色图像?

由于

6 个答案:

答案 0 :(得分: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();

答案 1 :(得分:3)

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

}

答案 2 :(得分:3)

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

答案 3 :(得分:0)

这里更新了Swift 4的代码并进行了一些安全检查。

lastPathComponent

答案 4 :(得分: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。只需将 tintColor 替换为 backgroundColor

如果您需要参数化的颜色:

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

答案 5 :(得分:0)

对于 Swift 5 :使用以下功能创建所需颜色的图像。然后使用这些图像将其设置为图像视图的动画属性:

extension UIImage {    
    func imageWithColor(_ color: UIColor) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale);
        guard let context = UIGraphicsGetCurrentContext(), let cgImage = self.cgImage else { return nil }

        context.translateBy(x: 0, y: self.size.height)
        context.scaleBy(x: 1.0, y: -1.0);
        context.setBlendMode(.normal)
        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        context.clip(to: rect, mask: cgImage)
        color.setFill()
        context.fill(rect)
        let  newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext();

        return newImage
    }
}


let animImages = [
    image0.imageWithColor(color),
    image1.imageWithColor(color),
    image2.imageWithColor(color),
].compactMap({ $0 })

imageView.animationImages = animImages
imageView.animationDuration = 0.7
imageView.animationRepeatCount = 0