Ios 用两种不同的颜色给图像着色

Ios 用两种不同的颜色给图像着色,ios,swift,uiimage,Ios,Swift,Uiimage,我有一张有两种颜色的图片:中间是红色的圆圈,周围是白色的圆环 有没有办法用两种不同的颜色给图像上色?例如:用绿色填充内圈,用蓝色填充外圈 是否可以仅更改外圆的颜色 为图像着色,如: let image = UIImage(named: "circles")?.tintWithColor(UIColor.red) 始终更改两个圆圈的颜色。您无法对图像进行这样的更改,因此请使用UIView 使用此自定义类 import UIKit @IBDesignable class CircleView:

我有一张有两种颜色的图片:中间是红色的圆圈,周围是白色的圆环

有没有办法用两种不同的颜色给图像上色?例如:用绿色填充内圈,用蓝色填充外圈

是否可以仅更改外圆的颜色

为图像着色,如:

let image = UIImage(named: "circles")?.tintWithColor(UIColor.red)

始终更改两个圆圈的颜色。

您无法对图像进行这样的更改,因此请使用UIView

使用此自定义类

import UIKit

@IBDesignable
class CircleView: UIView {

    @IBInspectable var strokeWidth: CGFloat = 0
    @IBInspectable var outerFillColor: UIColor = UIColor.clear
    @IBInspectable var innerFillColor: UIColor = UIColor.red
    @IBInspectable var strokeColor: UIColor = UIColor.clear
    @IBInspectable var innerWidth: CGFloat = 0

    @IBInspectable var bgColor: UIColor = UIColor.white {
        didSet {
            backgroundColor = bgColor
        }
    }


    override func draw(_ rect: CGRect) {
        self.backgroundColor = bgColor
        let circlePath = UIBezierPath(ovalIn: rect)

        let shapeLayer = CAShapeLayer()
        shapeLayer.path = circlePath.cgPath

        shapeLayer.fillColor = outerFillColor.cgColor

        shapeLayer.strokeColor = strokeColor.cgColor

        shapeLayer.lineWidth = strokeWidth

        self.layer.addSublayer(shapeLayer)

        let iFrame = CGRect(x: self.frame.width/2 - innerWidth/2,
                            y: self.frame.height/2 - innerWidth/2,
                            width: innerWidth, height: innerWidth)

        let innerCirclePath = UIBezierPath(ovalIn: iFrame)

        let shapeLayerInner = CAShapeLayer()
        shapeLayerInner.path = innerCirclePath.cgPath
        shapeLayerInner.fillColor = innerFillColor.cgColor
        self.layer.addSublayer(shapeLayerInner)
    }


}
如何使用

  • 从情节提要中获取UIView并分配类
  • 现在使用该属性

    • 行程宽度:外圆宽度

    • 笔划颜色:外圆颜色

    • 外圈填充颜色:外圈填充颜色

    • 内圆宽度:内圆宽度

    • 内圈填充颜色:内圈填充颜色

    • bgcolor:uiview的背景色,它已在情节提要中可用。这是额外的

  • 输出


    现在从情节提要中更改颜色并选中。属性会立即应用到情节提要上,因此您无需运行项目。

    您无法对图像进行这样的更改,因此请使用UIView

    使用此自定义类

    import UIKit
    
    @IBDesignable
    class CircleView: UIView {
    
        @IBInspectable var strokeWidth: CGFloat = 0
        @IBInspectable var outerFillColor: UIColor = UIColor.clear
        @IBInspectable var innerFillColor: UIColor = UIColor.red
        @IBInspectable var strokeColor: UIColor = UIColor.clear
        @IBInspectable var innerWidth: CGFloat = 0
    
        @IBInspectable var bgColor: UIColor = UIColor.white {
            didSet {
                backgroundColor = bgColor
            }
        }
    
    
        override func draw(_ rect: CGRect) {
            self.backgroundColor = bgColor
            let circlePath = UIBezierPath(ovalIn: rect)
    
            let shapeLayer = CAShapeLayer()
            shapeLayer.path = circlePath.cgPath
    
            shapeLayer.fillColor = outerFillColor.cgColor
    
            shapeLayer.strokeColor = strokeColor.cgColor
    
            shapeLayer.lineWidth = strokeWidth
    
            self.layer.addSublayer(shapeLayer)
    
            let iFrame = CGRect(x: self.frame.width/2 - innerWidth/2,
                                y: self.frame.height/2 - innerWidth/2,
                                width: innerWidth, height: innerWidth)
    
            let innerCirclePath = UIBezierPath(ovalIn: iFrame)
    
            let shapeLayerInner = CAShapeLayer()
            shapeLayerInner.path = innerCirclePath.cgPath
            shapeLayerInner.fillColor = innerFillColor.cgColor
            self.layer.addSublayer(shapeLayerInner)
        }
    
    
    }
    
    如何使用

  • 从情节提要中获取UIView并分配类
  • 现在使用该属性

    • 行程宽度:外圆宽度

    • 笔划颜色:外圆颜色

    • 外圈填充颜色:外圈填充颜色

    • 内圆宽度:内圆宽度

    • 内圈填充颜色:内圈填充颜色

    • bgcolor:uiview的背景色,它已在情节提要中可用。这是额外的

  • 输出


    现在从情节提要中更改颜色并选中。属性会立即应用到故事板上,因此您不需要运行项目。

    您可以显示您在ques中描述的图像吗?您不能更改图像,所以请使用UIView。为什么会被否决?您可以显示您在ques中描述的图像吗?您不能更改图像,所以请使用UIView。为什么会被否决?