关于ios:移除bezier路径外 | 珊瑚贝

Remove outside of bezier path


我有一个功能,我用颜色填充图像并使用 UIBezierPath 擦除角落的点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
CGRect rect = CGRectMake(0.0f, 0.0f, width, height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetBlendMode(context, kCGBlendModeCopy);

// Fill image
CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
CGContextFillRect(context, rect);

// Round corners
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:15.0];
CGContextSetStrokeColorWithColor(context, [[UIColor clearColor] CGColor]);
[bezierPath stroke];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

通过以上操作,我得到了一张切掉了 Bézier 路径并填充了背景的图像。
Icon with Bézier path cut out

但是,我怎样才能移除路径外的角落,或者至少获得某种方式来参考它们的位置以便我可以清除它们?

  • 您应该将图像蒙版到该路径,而不是抚摸路径。
  • 顺便说一句,确实需要创建带有圆角的图像,因为如果您只想要 UI 中的圆角,您可以设置视图 layer 的 cornerRadius。
  • @Rob我很遗憾不能用我这样做的方式改变视图本身,所以我必须使用图像。不过谢谢你,我会检查掩蔽。
  • @Rob我尝试从当前上下文中获取 UIImage,创建一个新上下文,并在那里应用贝塞尔路径,但这没有做任何事情(实际上,它使整个图像清晰)。我错过了什么吗?


几个选项:

  • 像你一样使用 CoreGraphics,但将其剪辑到路径:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    CGRect rect = CGRectMake(0.0f, 0.0f, width, height);

    UIGraphicsBeginImageContextWithOptions(rect.size, false, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetBlendMode(context, kCGBlendModeCopy);

    // Round corners
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:15.0];
    CGContextAddPath(context, bezierPath.CGPath);
    CGContextClip(context);

    // Fill image
    CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

  • 或者,消除CoreGraphics,只消除fill UIBezierPath:

    1
    2
    3
    4
    5
    UIGraphicsBeginImageContextWithOptions(rect.size, false, 0);
    [[UIColor redColor] setFill];
    [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:15.0] fill];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
  • 注意,在这两个示例中,我都使用了 UIGraphicsBeginImageContextWithOptions,提供了 0 的比例(针对相关设备上的显示进行了优化的比例)。如果你真的想要,你可以提供 1 的比例,这在 Retina 设备上渲染时显然会有点像素化,但这取决于你。

    • 后者在我的例子中效果很好,谢谢!


    来源:https://www.codenong.com/44488518/

    微信公众号
    手机浏览(小程序)

    Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

    Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

    Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9059.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
    0
    分享到:
    没有账号? 忘记密码?