2

I have an UIView that its layer contents is an image.

let image = UIImage(names: "myImage")
layer.contents = image.CGImage

This image has a few colors.

Is there a way to change a specific color to any other color of my choice?

I found answers for changing the all of the colors in the image but not a specific one.

answer

Community
  • 1
  • 1
ilan
  • 4,402
  • 6
  • 40
  • 76
  • 2
    Possible duplicate of [Want to change a particular color inside an image with another color - iPhone](http://stackoverflow.com/questions/7171679/want-to-change-a-particular-color-inside-an-image-with-another-color-iphone) – Vizllx Feb 09 '16 at 12:49
  • @dfri This isn't a Swift question. It has the [swift] tag, but it's a UIKit question and is being marked as a duplicate of another UIKit question. The programming language is irrelevant here because the question is about the framework and not about the language. – nhgrif Feb 09 '16 at 13:23
  • @LeoDabus- that's is what provided in the answer http://stackoverflow.com/a/2650835/2714702. – Vizllx Feb 09 '16 at 13:27
  • @LeoDabus- take a good look in it , it specifically mentioned about CGImageCreateWithMaskingColors , and no one will write whole code for a problem, the logic is already covered in the answer. For your kind information please read the answer properly the code link is also provided in that answer. http://stackoverflow.com/questions/633722/how-to-make-one-color-transparent-on-a-uiimage/1421422#1421422. That's why I say read properly before commenting. – Vizllx Feb 09 '16 at 16:55

2 Answers2

5

You can't change specific color in PNG with transparent background, but.. i found the solution.

extension UIImage {
    func maskWithColors(color: UIColor) -> UIImage? {
        let maskingColors: [CGFloat] = [100, 255, 100, 255, 100, 255] // We should replace white color.
        let maskImage = cgImage! //
        let bounds = CGRect(x: 0, y: 0, width: size.width * 3, height: size.height * 3) // * 3, for best resolution.
        let sz = CGSize(width: size.width * 3, height: size.height * 3) // Size.
        var returnImage: UIImage? // Image, to return

        /* Firstly we will remove transparent background, because
         maskingColorComponents don't work with transparent images. */

        UIGraphicsBeginImageContextWithOptions(sz, true, 0.0)
        let context = UIGraphicsGetCurrentContext()!
        context.saveGState()
        context.scaleBy(x: 1.0, y: -1.0) // iOS flips images upside down, this fix it.
        context.translateBy(x: 0, y: -sz.height) // and this :)
        context.draw(maskImage, in: bounds)
        context.restoreGState()
        let noAlphaImage = UIGraphicsGetImageFromCurrentImageContext() // new image, without transparent elements.
        UIGraphicsEndImageContext()

        let noAlphaCGRef = noAlphaImage?.cgImage // get CGImage.

        if let imgRefCopy = noAlphaCGRef?.copy(maskingColorComponents: maskingColors) { // Magic.
            UIGraphicsBeginImageContextWithOptions(sz, false, 0.0)
            let context = UIGraphicsGetCurrentContext()!
            context.scaleBy(x: 1.0, y: -1.0)
            context.translateBy(x: 0, y: -sz.height)
            context.clip(to: bounds, mask: maskImage) // Remove background from image with mask.
            context.setFillColor(color.cgColor) // set new color. We remove white color, and set red.
            context.fill(bounds)
            context.draw(imgRefCopy, in: bounds) // draw new image
            let finalImage = UIGraphicsGetImageFromCurrentImageContext()
            returnImage = finalImage! // YEAH!
            UIGraphicsEndImageContext()
        }
        return returnImage
    }
}

For call this function use code like this...

let image = UIImage(named: "Brush").maskWithColor(color: UIColor.red)

Result:

Before, and after using function.

Morgachev
  • 136
  • 2
  • 6
  • Material... https://developer.apple.com/library/content/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_images/dq_images.html – Morgachev Apr 08 '18 at 18:30
-4

You can not change the image color... The only way is to change the image on any event or something... Another variant is to create one image with transparent color, and set the background color of the view or something where you put the image...

Rutger Huijsmans
  • 2,330
  • 2
  • 30
  • 67