How can I color a UIImage in Swift?

后端 未结 20 2120
执念已碎
执念已碎 2020-11-29 18:27

I have an image called arrowWhite. I want to colour this image to black.

func attachDropDownArrow() -> NSMutableAttributedString {
    let im         


        
相关标签:
20条回答
  • 2020-11-29 18:56

    Swift 4

     let image: UIImage? =  #imageLiteral(resourceName: "logo-1").withRenderingMode(.alwaysTemplate)
        topLogo.image = image
        topLogo.tintColor = UIColor.white
    
    0 讨论(0)
  • 2020-11-29 18:57

    This function uses core graphics to achieve this.

    func overlayImage(color: UIColor) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(self.size, false, UIScreen.main.scale)
        let context = UIGraphicsGetCurrentContext()
    
        color.setFill()
    
        context!.translateBy(x: 0, y: self.size.height)
        context!.scaleBy(x: 1.0, y: -1.0)
    
        context!.setBlendMode(CGBlendMode.colorBurn)
        let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
        context!.draw(self.cgImage!, in: rect)
    
        context!.setBlendMode(CGBlendMode.sourceIn)
        context!.addRect(rect)
        context!.drawPath(using: CGPathDrawingMode.fill)
    
        let coloredImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    
        return coloredImage
    }
    
    0 讨论(0)
  • 2020-11-29 19:02

    For swift 4.2 to change UIImage color as you want (solid color)

    extension UIImage {
        func imageWithColor(color: UIColor) -> UIImage {
            UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
            color.setFill()
    
            let context = UIGraphicsGetCurrentContext()
            context?.translateBy(x: 0, y: self.size.height)
            context?.scaleBy(x: 1.0, y: -1.0)
            context?.setBlendMode(CGBlendMode.normal)
    
            let rect = CGRect(origin: .zero, size: CGSize(width: self.size.width, height: self.size.height))
            context?.clip(to: rect, mask: self.cgImage!)
            context?.fill(rect)
    
            let newImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
    
            return newImage!
        }
    }
    

    How to use

    self.imgVw.image = UIImage(named: "testImage")?.imageWithColor(UIColor.red)
    
    0 讨论(0)
  • 2020-11-29 19:02

    Post iOS 13 you can use it something like this

    arrowWhiteImage.withTintColor(.black, renderingMode: .alwaysTemplate)
    
    0 讨论(0)
  • 2020-11-29 19:02

    I have modified the extension found here: Github Gist, for Swift 3 which I have tested in the context of an extension for UIImage.

    func tint(with color: UIColor) -> UIImage 
    {
       UIGraphicsBeginImageContext(self.size)
       guard let context = UIGraphicsGetCurrentContext() else { return self }
    
       // flip the image
       context.scaleBy(x: 1.0, y: -1.0)
       context.translateBy(x: 0.0, y: -self.size.height)
    
       // multiply blend mode
       context.setBlendMode(.multiply)
    
       let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
       context.clip(to: rect, mask: self.cgImage!)
       color.setFill()
       context.fill(rect)
    
       // create UIImage
       guard let newImage = UIGraphicsGetImageFromCurrentImageContext() else { return self }
       UIGraphicsEndImageContext()
    
       return newImage
    }
    
    0 讨论(0)
  • 2020-11-29 19:04

    Create an extension on UIImage:

    /// UIImage Extensions
    extension UIImage {
        func maskWithColor(color: UIColor) -> UIImage {
    
            var maskImage = self.CGImage
            let width = self.size.width
            let height = self.size.height
            let bounds = CGRectMake(0, 0, width, height)
    
            let colorSpace = CGColorSpaceCreateDeviceRGB()
            let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue)
            let bitmapContext = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, colorSpace, bitmapInfo)
    
            CGContextClipToMask(bitmapContext, bounds, maskImage)
            CGContextSetFillColorWithColor(bitmapContext, color.CGColor)
            CGContextFillRect(bitmapContext, bounds)
    
            let cImage = CGBitmapContextCreateImage(bitmapContext)
            let coloredImage = UIImage(CGImage: cImage)
    
            return coloredImage!
        }
    }
    

    Then you can use it like that:

    image.maskWithColor(UIColor.redColor())
    
    0 讨论(0)
提交回复
热议问题