如何在Swift中为UIImage着色?

Suj*_* Os 81 image colors uiimage ios swift

我有一个叫做的图像arrowWhite.我想将此图像着色为黑色.

func attachDropDownArrow() -> NSMutableAttributedString {
        let image:UIImage = UIImage(named: "arrowWhite.png")!
        let attachment = NSTextAttachment()
        attachment.image = image
        attachment.bounds = CGRectMake(2.25, 2, attachment.image!.size.width - 2.25, attachment.image!.size.height - 2.25)
        let attachmentString = NSAttributedString(attachment: attachment)
        let myString = NSMutableAttributedString(string: NSString(format: "%@", self.privacyOptions[selectedPickerRow]) as String)
        myString.appendAttributedString(attachmentString)
        return myString
    }
Run Code Online (Sandbox Code Playgroud)

我希望得到这张图片blackColour.
tintColor不管用...

kuz*_*zdu 148

斯威夫特4

extension UIImageView {
  func setImageColor(color: UIColor) {
    let templateImage = self.image?.withRenderingMode(.alwaysTemplate)
    self.image = templateImage
    self.tintColor = color
  }
}
Run Code Online (Sandbox Code Playgroud)

像这样打电话:

let imageView = UIImageView(image: UIImage(named: "your_image_name"))
imageView.setImageColor(color: UIColor.purple)
Run Code Online (Sandbox Code Playgroud)

Alternativ For Swift 3和4

extension UIImage {

    func maskWithColor(color: UIColor) -> UIImage? {
        let maskImage = cgImage!

        let width = size.width
        let height = size.height
        let bounds = CGRect(x: 0, y: 0, width: width, height: height)

        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
        let context = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)!

        context.clip(to: bounds, mask: maskImage)
        context.setFillColor(color.cgColor)
        context.fill(bounds)

        if let cgImage = context.makeImage() {
            let coloredImage = UIImage(cgImage: cgImage)
            return coloredImage
        } else {
            return nil
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

对于Swift 2.3

extension UIImage {
func maskWithColor(color: UIColor) -> UIImage? {

    let 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(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue)
    let bitmapContext = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, colorSpace, bitmapInfo.rawValue) //needs rawValue of bitmapInfo

    CGContextClipToMask(bitmapContext, bounds, maskImage)
    CGContextSetFillColorWithColor(bitmapContext, color.CGColor)
    CGContextFillRect(bitmapContext, bounds)

    //is it nil?
    if let cImage = CGBitmapContextCreateImage(bitmapContext) {
        let coloredImage = UIImage(CGImage: cImage)

        return coloredImage

    } else {
        return nil
    } 
 }
}
Run Code Online (Sandbox Code Playgroud)

像这样打电话:

let image = UIImage(named: "your_image_name")
testImage.image =  image?.maskWithColor(color: UIColor.blue)
Run Code Online (Sandbox Code Playgroud)

  • 不错的开始,但结果是颗粒感.正如@Darko在下面提到的那样,我认为这是因为你没有考虑规模和其他参数. (8认同)
  • 不保留缩放和方向. (4认同)

Nik*_*uhe 86

有一个内置的方法来获取一个UIImage模板模式下自动呈现的方法.这使用视图的tintColor为图像着色:

let templateImage = originalImage.imageWithRenderingMode(UIImageRenderingModeAlwaysTemplate)
myImageView.image = templateImage
myImageView.tintColor = UIColor.orangeColor()
Run Code Online (Sandbox Code Playgroud)

  • 有关渲染模式,请参阅Swift 3语法:http://stackoverflow.com/a/24145287/448718 (6认同)
  • 使用imageview是显而易见的,但我们只想要UIImage (6认同)
  • 如果您独立于UIImageView使用UIImage对象,则不是解决方案.这仅在您有权访问UIImageView时才有效 (3认同)
  • 这是最好的答案 - 可以在Apple Docs中找到更多信息 - https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/UIKitUICatalog/#//apple_ref/doc/uid/TP40012857-UIView- SW17 (2认同)

Har*_*oom 38

首先,您必须将图像的渲染属性更改为.xcassets文件夹中的"模板图像".然后,您可以像这样更改UIImageView实例的tint颜色属性:

imageView.tintColor = UIColor.whiteColor()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Ale*_*bin 25

我最终得到了这个,因为其他答案要么失去解决方案,要么使用UIImageView,而不是UIImage,或者包含不必要的操作:

斯威夫特3

extension UIImage {

    public func maskWithColor(color: UIColor) -> UIImage {

        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
        let context = UIGraphicsGetCurrentContext()!

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

        color.setFill()
        self.draw(in: rect)

        context.setBlendMode(.sourceIn)
        context.fill(rect)

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

}
Run Code Online (Sandbox Code Playgroud)

  • 这里的最佳答案,保持相同的图像方向和质量 (4认同)

小智 21

此功能使用核心图形来实现此目的.

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
}
Run Code Online (Sandbox Code Playgroud)

  • 这是有效的,其他前两个答案是错误的. (8认同)
  • 是的,这完美无缺.`maskWithColor`扩展可以工作但忽略`scale`,因此在更高分辨率的设备上图像看起来不清晰. (4认同)

Dar*_*rko 10

在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!
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你就可以这样使用它:

image.maskWithColor(UIColor.redColor())
Run Code Online (Sandbox Code Playgroud)


Pez*_*Pez 9

我发现HR的解决方案最有帮助,但稍微适应了Swift 3

extension UIImage {

    func maskWithColor( 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)

         let rect = CGRect(x: 0.0, y: 0.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!
    }
}
Run Code Online (Sandbox Code Playgroud)

这考虑了比例并且也不像其他一些解决方案那样产生较低的res图像.用法:

image = image.maskWithColor(color: .green )
Run Code Online (Sandbox Code Playgroud)


小智 8

For iOS13+ there are withTintColor(__:) and withTintColor(_:renderingMode:) methods.

Example usage:

let newImage = oldImage.withTintColor(.red)
Run Code Online (Sandbox Code Playgroud)

or

let newImage = oldImage.withTintColor(.red, renderingMode: .alwaysTemplate)
Run Code Online (Sandbox Code Playgroud)


Har*_*kar 7

对于Swift 4.2可以根据需要更改UIImage颜色

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!
    }
}
Run Code Online (Sandbox Code Playgroud)

如何使用

self.imgVw.image = UIImage(named: "testImage")?.imageWithColor(UIColor.red)
Run Code Online (Sandbox Code Playgroud)


Daz*_*ong 5

来自@Nikolai Ruhe的Swift 3扩展包装回答.

extension UIImageView {

    func maskWith(color: UIColor) {
        guard let tempImage = image?.withRenderingMode(.alwaysTemplate) else { return }
        image = tempImage
        tintColor = color
    }

}
Run Code Online (Sandbox Code Playgroud)

它也可以用UIButton,例如:

button.imageView?.maskWith(color: .blue)
Run Code Online (Sandbox Code Playgroud)


Alf*_*lfi 5

斯威夫特 4

 let image: UIImage? =  #imageLiteral(resourceName: "logo-1").withRenderingMode(.alwaysTemplate)
    topLogo.image = image
    topLogo.tintColor = UIColor.white
Run Code Online (Sandbox Code Playgroud)


Raj*_*han 5

在您的代码中添加此扩展并在故事板本身中更改图像颜色。

斯威夫特 4 和 5:

extension UIImageView {
    @IBInspectable
    var changeColor: UIColor? {
        get {
            let color = UIColor(cgColor: layer.borderColor!);
            return color
        }
        set {
            let templateImage = self.image?.withRenderingMode(.alwaysTemplate)
            self.image = templateImage
            self.tintColor = newValue
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

故事板预览:

在此处输入图片说明


Fat*_*tie 5

简单的方法:

yourIcon.image = yourIcon.image?.withRenderingMode(.alwaysTemplate)
yourIcon.tintColor = .someColor
Run Code Online (Sandbox Code Playgroud)

顺便说一句,在 Android 上更有趣!

yourIcon.setColorFilter(getColor(R.color.someColor), PorterDuff.Mode.MULTIPLY);
Run Code Online (Sandbox Code Playgroud)