如何创建一个代表UIImage径向部分的UIImage?

时间:2015-07-04 00:46:47

标签: ios uiimage

如下图所示,百分比为25%。如何生成另一个代表给定图像径向部分25%的UIImage?非常感谢任何帮助。

输入 input image 产量 output image

- (UIImage *)radialPortion:(float)percent image:(UIImage *)image;

1 个答案:

答案 0 :(得分:1)

- (UIImage *)radialPortion:(float)percent image:(UIImage *)image
{
    CGSize size = image.size;
    CGRect rect = {CGPointZero,size};
    UIGraphicsBeginImageContext(size);
    CGFloat radius = MAX(size.width, size.height)/2;
    UIBezierPath *path = [UIBezierPath bezierPath];
    CGPoint center = CGPointMake(size.width/2, size.height/2);
    [path moveToPoint:center];
    [path addLineToPoint:CGPointMake(center.x, center.y-radius)];
    [path addArcWithCenter:center radius:radius startAngle:-M_PI_2 endAngle:-M_PI_2+M_PI*2*percent clockwise:1];
    [path closePath];
    [path addClip];
    [image drawInRect:rect];
    return UIGraphicsGetImageFromCurrentImageContext();
}

注意:代码仅适用于样本图像,可能不适用于其他图像。