【问题标题】:UIBezierPath curve from far edges来自远边缘的 UIBezierPath 曲线
【发布时间】:2013-11-26 07:54:23
【问题描述】:

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

这是我目前编写的代码。我唯一的问题是这种方法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;
}

更新:

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

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];

【问题讨论】:

    标签: ios uibezierpath


    【解决方案1】:

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

    【讨论】:

    • 我是 iOS 领域的新手,不完全了解所有参数是什么。你能用示例代码更新你的答案,这样我就可以让它工作,然后我就可以从那里调整它。谢谢
    • @cnotethegr8 这可能是一个很好的机会来看看the documentation
    • @DavidRönnqvist 文档对我没有帮助。它是我现在需要的细节。我不确定如何完成曲线。我会用我更新的代码更新我的问题。
    【解决方案2】:

    这是我的解决方案,供任何想查看示例代码的人使用。

    - (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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      相关资源
      最近更新 更多