Iphone iOS中的外部填充颜色
我有两个矩形(两个闭合的子路径)。 矩形B很小,位于矩形A内。矩形A内的所有边。 有没有直接的方法来填充矩形B外部的颜色区域Iphone iOS中的外部填充颜色,iphone,ios,cgpath,cashapelayer,Iphone,Ios,Cgpath,Cashapelayer,我有两个矩形(两个闭合的子路径)。 矩形B很小,位于矩形A内。矩形A内的所有边。 有没有直接的方法来填充矩形B外部的颜色区域 CAShapeLayer fillExternalColor类似的东西?如果不是直接的方法,如何通过编程实现 A-紫色 黄色 所以我试着画A,然后画B。我希望B是清晰的颜色(现在我用黄色),但后来我看到了A的紫色 我找到了给出AnB的CGRectIntersection方法和给出AuB的CGRectUnion方法。 是否有一种方法可以给出该区域的其余部分,即AuB-A
CAShapeLayer fillExternalColor
类似的东西?如果不是直接的方法,如何通过编程实现
A-紫色
黄色
所以我试着画A,然后画B。我希望B是清晰的颜色(现在我用黄色),但后来我看到了A的紫色
我找到了给出AnB的CGRectIntersection方法和给出AuB的CGRectUnion方法。
是否有一种方法可以给出该区域的其余部分,即AuB-AnB kCAFillRuleEvenOdd 指定奇偶缠绕规则。计算路径交叉点的总数。如果交叉点的数量为偶数,则该点位于路径之外。如果交叉点的数量为奇数,则该点位于路径内,且包含该点的区域应填充 在OS X v10.6及更高版本中提供 在CAShapeLayer.h中声明
参考:假设A是较大的形状,B是较小的内部形状,边界指A的包含矩形(可能是视图的边界)
HoleView:UIView
必须设置:
self.opaque = NO;
self.backgroudColor = [UIColor clearColor];
在drawRect:
[[UIColor purpleColor] setFill];
UIRectFill(A);
CGRect gapRectIntersection = CGRectIntersection(B, A);
[[UIColor clearColor] setFill];
UIRectFill(gapRectIntersection);
您使用的是
CAShapeLayer
。如果您的fillColor
和strokeColor
都是不透明的(alpha 1.0),您可以简单地设置层的backgroundColor
,以填充层边界内但其笔划和填充路径外的像素
我的测试代码:
@implementation ViewController {
CAShapeLayer *layer;
}
- (void)viewDidLoad {
[super viewDidLoad];
layer = [CAShapeLayer layer];
layer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 250, 250)].CGPath;
layer.fillColor = [UIColor yellowColor].CGColor;
layer.strokeColor = [UIColor whiteColor].CGColor;
layer.lineWidth = 4;
layer.backgroundColor = [UIColor purpleColor].CGColor;
[self.view.layer addSublayer:layer];
}
- (void)viewDidLayoutSubviews {
layer.frame = self.view.bounds;
}
@end
结果:
我想在UIImage上添加带有遮罩外部的红色边框内矩形 我浏览了这一页。下面的代码使用的是CGContextEOFillPath,对像我这样的人很有帮助。(部分代码是从其他页面收集的。)
问候。你说的是在矩形外画图,对吗?你只是想填充矩形A?你需要知道外边界…@Undo是的。。。在B外但在A内绘制颜色为什么不填充A,然后在其上绘制B?@Laws。。。请看一看屏幕截图。当你说addClip时,你能详细说明一下吗?很抱歉,我不能按照你的建议去做。我有一个CGRect innerRect和CGRect outerRect。我在一个独立的方法中绘制东西,该方法从主线程dispatch_sync(dispatch_get_main_queue(),^{[self-displayRectangles:rectangles with lineColor:lineColor];})运行;稍后,我创建了一个CAShapeLayer,并将该层作为子层添加到视图中。
-(UIImage ) imageByDrawingBorderRectOnImage:(UIImage )image theRect:(CGRect)theRect
{
// begin a graphics context of sufficient size
UIGraphicsBeginImageContext(image.size);
// draw original image into the context
[image drawAtPoint:CGPointZero];
// get the context for CoreGraphics
CGContextRef ctx = UIGraphicsGetCurrentContext();
// set stroking color and to draw rect
[[UIColor redColor] setStroke];
// drawing with a red stroke color
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
// the line width to 3
CGContextSetLineWidth(ctx, 3.0);
// Add Stroke Rectangle,
CGContextStrokeRect(ctx, theRect);
// Now draw fill outside part with partial alpha gray color
// drawing with a gray stroke color
CGMutablePathRef aPath = CGPathCreateMutable();
// outer rectangle
CGRect rectangle = CGRectMake( 0, 0, image.size.width, image.size.height);
CGPathAddRect(aPath, nil, rectangle);
// innter rectangle
CGPathAddRect(aPath, nil, theRect);
// set gray transparent color
CGContextSetFillColorWithColor(ctx, [UIColor colorWithRed:0.75 green:0.75 blue:0.75 alpha:0.5].CGColor);
// add the path to Context
CGContextAddPath(ctx, aPath);
// This method uses Even-Odd Method to draw in outer rectangle
CGContextEOFillPath(ctx);
// make image out of bitmap context
UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext();
// free the context
UIGraphicsEndImageContext();
return retImage;
}