iOS:如何使用百分比

时间:2015-07-13 14:28:37

标签: ios uiimageview uiimage

我想用百分比填充图像(例如与评级相同)。

  • 实际上我正在显示带浮动值的评级图像。
  • 取决于浮动值我需要用特定图像填充。

我得到的结果可以填满所有图像。但我的疑问是用百分比值填充图像。

我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    _ColImgView.image = [self imageNamed:@"star.png" withColor:[UIColor redColor]];
}

- (UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color
{
    // load the image

    UIImage *img = [UIImage imageNamed:name];

    // begin a new image context, to draw our colored image onto
    UIGraphicsBeginImageContext(img.size);

    // get a reference to that context we created
    CGContextRef context = UIGraphicsGetCurrentContext();

    // set the fill color
    [color setFill];

    // translate/flip the graphics context (for transforming from CG* coords to UI* coords
    CGContextTranslateCTM(context, 0, img.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // set the blend mode to color burn, and the original image
    CGContextSetBlendMode(context, kCGBlendModeColorBurn);
    CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
    CGContextDrawImage(context, rect, img.CGImage);

    // set a mask that matches the shape of the image, then draw (color burn) a colored rectangle
    CGContextClipToMask(context, rect, img.CGImage);
    CGContextAddRect(context, rect);
    CGContextDrawPath(context,kCGPathFill);

    // generate a new UIImage from the graphics context we drew onto
    UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //return the color-burned image
    return coloredImg;
}

预览图片

enter image description here

1 个答案:

答案 0 :(得分:0)

您必须添加叠加层。叠加应该在图像下,图像应该是透明的png。要在指定区域填充颜色覆盖,您可以使用此处的代码: iOS - Fill bezierPath with multiple colors based on percentage

如果我找空闲时间,我会为你编写代码,但现在我只能解释你应该做什么。

相关问题