简体   繁体   中英

How to change color of the UIImage

I am trying to change the color of the particular UIImage but when i search in the internet i ended up with result of changing tint color of the UIImage, If i change the tint color it will overlap the entire image with the Given color this not what i needed. Can someone please help me to do this? This may look like doing the photo editing in the Photoshop or sketch. I have attached the image below what exactly i am looking for.

给定图像 .

For (eg) above image i have as a layer (UIImageView), If i change the color of the OverCoat image that will change into the color what i set.

改变颜色后

After changing the color simply it will look like the above.

Can someone please help me to do this.

One way to accomplish this would be to use a mask image that defines which areas of the image should be tinted:

用蒙版着色的图像

This can be accomplished via UIGraphicsImageRenderer , see Tinting an UIImage with a mask for an example playground.

import UIKit

let image = UIImage(named: "landscape.jpg")!
let mask = UIImage(named: "mask.png")!

let imageRenderer = UIGraphicsImageRenderer(size: image.size)

let tintedImage = imageRenderer.image { (ctx) in
    let cgContext = ctx.cgContext
    let rect = CGRect(origin: .zero, size: image.size)
    image.draw(in: rect)
    cgContext.saveGState()
    cgContext.translateBy(x: 0, y: rect.size.height)
    cgContext.scaleBy(x: 1.0, y: -1.0);
    cgContext.clip(to: rect, mask: mask.cgImage!)
    cgContext.setBlendMode(.color)
    UIColor.red.setFill()
    cgContext.fill(rect)
    cgContext.restoreGState()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM