删除贝塞尔路径之外
Remove outside of bezier path
我有一个功能,我用颜色填充图像并使用 UIBezierPath
擦除角点。
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();
通过上面的操作,我得到了一张确实切掉了贝塞尔路径并填充了背景的图像。
但是,我怎样才能删除路径外的角,或者至少找到一些方法来参考它们的位置以便我可以清除它们?
几个选项:
像您一样使用 CoreGraphics,但将其剪辑到路径:
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
:
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0);
[[UIColor redColor] setFill];
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:15.0] fill];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
请注意,在这两个示例中,我都使用了 UIGraphicsBeginImageContextWithOptions
,提供了 0
的比例(为在相关设备上显示而优化的比例)。如果你真的想要,你可以提供 1
的比例,在视网膜设备上渲染时显然会有点像素化,但这取决于你。
我有一个功能,我用颜色填充图像并使用 UIBezierPath
擦除角点。
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();
通过上面的操作,我得到了一张确实切掉了贝塞尔路径并填充了背景的图像。
但是,我怎样才能删除路径外的角,或者至少找到一些方法来参考它们的位置以便我可以清除它们?
几个选项:
像您一样使用 CoreGraphics,但将其剪辑到路径:
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
:UIGraphicsBeginImageContextWithOptions(rect.size, false, 0); [[UIColor redColor] setFill]; [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:15.0] fill]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
请注意,在这两个示例中,我都使用了 UIGraphicsBeginImageContextWithOptions
,提供了 0
的比例(为在相关设备上显示而优化的比例)。如果你真的想要,你可以提供 1
的比例,在视网膜设备上渲染时显然会有点像素化,但这取决于你。