简体   繁体   中英

How to fill with color the transparent part of the UIImage?

I have a PNG image with a transparent area and background.

在此处输入图片说明

I want to use it as SKTexture for my SKSpriteNode, but before it should be colored in some UIColor.

I'm trying to achieve this with

func colorImage(origImage: UIImage, color: UIColor) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(origImage.size, true, 0)
    let context = UIGraphicsGetCurrentContext()
    context?.setFillColor(color.cgColor)
    context?.fill(CGRect(origin: CGPoint(x: 0, y: 0), size: origImage.size))
    let flipVertical = CGAffineTransform.init(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: origImage.size.height)
    context?.concatenate(flipVertical)
    context?.draw(origImage.cgImage!, in: CGRect(origin: CGPoint(x: 0, y: 0), size: origImage.size))
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image!
}

But it colors the background too:(

在此处输入图片说明

How can I color only the cloud?

Any help is appreciated!

For this it would be better to just use the cloud texture as is an then color blend your SKSpriteNode.

https://developer.apple.com/reference/spritekit/skspritenode/1519639-color

Note you'll need to set the colorBlendFactor as well. The default is 0.

https://developer.apple.com/reference/spritekit/skspritenode/1519780-colorblendfactor

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