ios:我需要填充一半的UIImage

时间:2018-12-16 20:51:41

标签: ios swift graphics

我有一个star的UIImage。

我需要以编程方式用蓝色填充一半。 我试试这个:

extension UIImage {

public func maskWithColor(color: UIColor, factor: CGFloat) -> UIImage 
{

        UIGraphicsBeginImageContextWithOptions(CGSize(width: 
        self.size.width/2, height: self.size.height), false, 
        self.scale)
        let context = UIGraphicsGetCurrentContext()!

        let rect = CGRect(origin: CGPoint.zero, size: size)

        color.setFill()
        self.draw(in: rect)

        context.setBlendMode(.sourceIn)
        context.fill(rect)

        let resultImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return resultImage
    }

}

其结果:   Half stars

但是结果如下: this

我需要一半-蓝色和其他灰色。我该怎么做?

1 个答案:

答案 0 :(得分:0)

安装此吊舱

pod 'Cosmos', '~> 17.0'

像这样使用

1)将“视图”对象从“对象库”拖到情节提要中。 在属性检查器中添加视图控件 Enter image description here

2)在Identity Inspector中将视图的类设置为CosmosView。如果您使用Carthage或CocoaPods设置方法,请将其模块属性设置为Cosmos。 将Cosmos评级视图添加到情节提要 Enter image description here

3)在“属性”检查器中自定义Cosmos视图外观。如果情节提要板未显示星星,请单击“编辑器”菜单中的“刷新所有视图”。 在Xcode的属性检查器中自定义宇宙外观。 Enter image description here