UIImage animationImages tint color?

前端 未结 6 1788
醉话见心
醉话见心 2020-12-31 13:58

Is there a way to tint the images in an animation?

I know I can tint a single image like this:

var imageOne:UIImage = UIImage(named: \"pullto_1.png\"         


        
6条回答
  •  一个人的身影
    2020-12-31 14:31

    Ok, i hoped that there is a simpler solution but this is what I ended up doing:

    This function will create a new image with the wanted color:

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

    And then you can call it like this:

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

提交回复
热议问题