联盟UIBezierPaths而不是安装path

我有一个应用程序,我采取UIBezierPath并通过一系列appendPath:调用将其用作画笔。 经过几次,并与真正复杂的刷形状内存耗尽,应用程序停下来。 我真正想做的是完全像Paint Code一样的联合,但我找不到任何这样做的方式。

我将如何去结合两个或两个以上的UIBezierPath?

编辑:

这是我想要dynamic实现的一个视觉。

在“绘制代码”中,您需要两条path,并将它们重叠: 油漆代码中重叠的路径

但我想合并/联合成一个新的单一path,如:

在“Paint Code”中合并路径

请注意,在代码的底部面板现在有一个单一的形状代码,这就是我想能够以编程方式可能有1000个原始path。

您可以通过以下两个核心graphics的概念轻松地获得所需的结果: –

i) CGBlendMode ii)OverLap2Layer

混合模式告诉上下文如何将新内容应用于自身。 他们决定像素数据是如何数字混合的。

class UnionUIBezierPaths : UIView { var firstBeizerPath:UIImage! var secondBeizerPath:UIImage! override func draw(_ rect: CGRect) { super.draw(rect) firstBeizerPath = drawOverLapPath(firstBeizerpath: drawCircle(), secondBeizerPath: polygon()) secondBeizerPath = drawOverLapPath(firstBeizerpath: polygon(), secondBeizerPath: drawCircle()) let image = UIImage().overLap2Layer(firstLayer:firstBeizerPath , secondLayer:secondBeizerPath) } func drawCircle() -> UIBezierPath { let path = UIBezierPath(ovalIn: CGRect(x: 40, y: 120, width: 100, height: 100) ) return path } func polygon() -> UIBezierPath { let beizerPath = UIBezierPath() beizerPath.move(to: CGPoint(x: 100, y: 10) ) beizerPath.addLine(to: CGPoint(x: 200.0, y: 40.0) ) beizerPath.addLine(to: CGPoint(x: 160, y: 140) ) beizerPath.addLine(to: CGPoint(x: 40, y: 140) ) beizerPath.addLine(to: CGPoint(x: 0, y: 40) ) beizerPath.close() return beizerPath } func drawOverLapPath(firstBeizerpath:UIBezierPath ,secondBeizerPath:UIBezierPath ) -> UIImage { UIGraphicsBeginImageContext(self.frame.size) let firstpath = firstBeizerpath UIColor.white.setFill() UIColor.black.setStroke() firstpath.stroke() firstpath.fill() // sourceAtop = 20 let mode = CGBlendMode(rawValue:20) UIGraphicsGetCurrentContext()!.setBlendMode(mode!) let secondPath = secondBeizerPath UIColor.white.setFill() UIColor.white.setStroke() secondPath.fill() secondPath.stroke() let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image! } func drawImage(image1:UIImage , secondImage:UIImage ) ->UIImage { UIGraphicsBeginImageContext(self.frame.size) image1.draw(in: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height) ) secondImage.draw(in: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height) ) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! } } //OverLap2Layer extension UIImage { func overLap2Layer(firstLayer:UIImage , secondLayer:UIImage ) -> UIImage { UIGraphicsBeginImageContext(firstLayer.size) firstLayer.draw(in: CGRect(x: 0, y: 0, width: firstLayer.size.width, height: firstLayer.size.height) ) secondLayer.draw(in: CGRect(x: 0, y: 0, width: firstLayer.size.width, height: firstLayer.size.height) ) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! } } 

第一条path: –

在这里输入图像说明

第二条path: –

在这里输入图像说明

最后结果 :-

在这里输入图像说明

参考 : – 核心graphics混合 , 创build图像

GitHub演示

您可以使用GPCPolygon ,一个用于GPC的Objective-C包装器

-GPCPolygonSet*) initWithPolygons:(NSMutableArray*)points;

要么

- (GPCPolygonSet*) unionWithPolygonSet:(GPCPolygonSet*)p2;