UIImage animationImages tint color?

前端 未结 6 1769
醉话见心
醉话见心 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:30

    Here's updated code for Swift 4 with a few safety checks.

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

提交回复
热议问题