Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I color a UIImage in Swift?

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

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 } 

I want to get this image in blackColour.
tintColor is not working...

like image 347
Sujisha Os Avatar asked Aug 04 '15 07:08

Sujisha Os


People also ask

Can we change image color in Swift?

The absolute simplest way to change colors of images (or icons in this case) is to use the SF Symbols where applicaple. This is a set of symbols Apple provides that can easily be used in your own app. You can download an app to help you find the correct symbol for you needs here.

How do I change the color of a PNG in Swift?

If you are setting the image for a button, just go to attributes inspector and change the button type to system. Then set the image and change the tint color. The color of the image will change.

How do I add color in Swift?

There are two ways to use your custom colors in Swift UI. Select your object in device preview. Choose “Color” under the attributes inspector. Your custom colors now show at the bottom of the list!

How do I change the UIImage color in Objective C?

setBlendMode(. multiply) ctx. draw(image. cgImage!, in: imageRect) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! } }


1 Answers

Swift 4 and 5

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

Call like this:

let imageView = UIImageView(image: UIImage(named: "your_image_name")) imageView.setImageColor(color: UIColor.purple) 

Alternativ For Swift 3, 4 or 5

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

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

Call like this:

let image = UIImage(named: "your_image_name") testImage.image =  image?.maskWithColor(color: UIColor.blue) 
like image 103
kuzdu Avatar answered Oct 22 '22 06:10

kuzdu