来自远边的UIBezierPath曲线

时间:2013-11-26 07:54:24

标签: ios uibezierpath

在iOS中,我正在尝试绘制剪裁的图像。我希望裁剪是从一个边缘,弯曲到最远的相对边缘。下图以红色显示最终图像的边框。在此示例中,曲线从左下角到右上角。

enter image description here

这是我目前编写的代码。我唯一的问题是这个方法bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:没有曲线到我需要的范围。

- (UIImage *)roundCorneredImage:(UIImage *)image radius:(CGFloat)radius {
    CGRect imageRect = CGRectZero;
    imageRect.size = image.size;

    UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, [UIScreen mainScreen].scale);

    CGSize size = CGSizeMake(radius, radius);
    [[UIBezierPath bezierPathWithRoundedRect:imageRect byRoundingCorners:UIRectCornerBottomRight cornerRadii:size] addClip];

    [image drawInRect:imageRect];

    UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return result;
}

更新:

这是我得到的结果和我更新的代码。我需要一些进一步的帮助...

enter image description here

UIBezierPath* bezier = [UIBezierPath bezierPath];
[bezier moveToPoint:CGPointMake(0, 0)];
[bezier addLineToPoint:CGPointMake(0, imageRect.size.height)];

[bezier addArcWithCenter:CGPointMake(imageRect.size.width / 2, imageRect.size.height / 2)
                  radius:imageRect.size.height / 2
              startAngle:M_PI / 2
                endAngle:0
               clockwise:NO];

[bezier addLineToPoint:CGPointMake(0, 0)];
    [bezier addClip];

2 个答案:

答案 0 :(得分:1)

添加addLineToPoint的直线和addArcWithCenter:radius:startAngle:endAngle:clockwise:的曲线,以便控制曲线的形状。

答案 1 :(得分:1)

对于想要查看示例代码的任何其他人来说,这是我的解决方案。

- (UIImage *)roundCorneredImage:(UIImage *)image radius:(CGFloat)radius {
    CGRect imageRect = CGRectZero;
    imageRect.size = image.size;

    UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, [UIScreen mainScreen].scale);

    UIBezierPath* bezier = [UIBezierPath bezierPath];
    [bezier moveToPoint:CGPointMake(imageRect.size.width, 0)];
    [bezier addLineToPoint:CGPointMake(0, 0)];
    [bezier addLineToPoint:CGPointMake(0, imageRect.size.height)];
    [bezier addArcWithCenter:CGPointMake(0, 0) radius:imageRect.size.height startAngle:M_PI / 2 endAngle:0 clockwise:NO];
    [bezier addClip];

    [image drawInRect:imageRect];

    UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return result;
}