如何更改UIImage的颜色

问题描述 投票:-1回答:1

我正在尝试更改特定UIImage的颜色,但是当我在互联网上搜索时,最终导致更改了UIImage的着色颜色的结果,如果我更改了着色颜色,它将与给定颜色重叠整个图像,不是我需要的。有人可以帮我做到这一点吗?这看起来就像在Photoshop或草图中进行照片编辑一样。我已在我要寻找的图像下面附加了图像。

Given Image

对于(例如,在我上面有一层图像(UIImageView),如果我更改了OverCoat图像的颜色,它将变为我设置的颜色)。>

After changing the color

更改颜色后,看起来就像上面的一样。

有人可以帮我做这个吗?

我正在尝试更改特定UIImage的颜色,但是当我在互联网上搜索时,最终结果是更改了UIImage的着色颜色,如果我更改了着色颜色,它将与...重叠。...]] >>

实现此目的的一种方法是使用遮罩图像,该图像定义应对图像的哪些区域进行着色:

Image tinted with mask

这可以通过UIGraphicsImageRenderer完成,请参见Tinting an UIImage with a mask以获取示例操场。

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()
}
ios swift iphone uiimageview uiimage
1个回答
0
投票

实现此目的的一种方法是使用遮罩图像,该图像定义应对图像的哪些区域进行着色:

© www.soinside.com 2019 - 2024. All rights reserved.