gpt4 book ai didi

ios - 联合 UIBezierPath 而不是追加路径

转载 作者:技术小花猫 更新时间:2023-10-29 10:39:30 24 4
gpt4 key购买 nike

我有一个应用程序,我在其中使用 UIBezierPath 并通过一系列 appendPath: 调用将其用作画笔。几次之后,使用非常复杂的画笔形状,内存就会耗尽,应用程序就会停止运行。我真正想做的是像 Paint Code 那样完全结合,但我找不到任何方法来做到这一点。

我将如何合并两个或多个 UIBezierPaths?

编辑:

这是我想要动态实现的视觉效果。

在 Paint Code 中,您采用两条路径并将它们重叠,如下所示: Overlapping paths in Paint Code

但我想将它们合并/合并到一个新的单一路径中,例如:

Merged paths in Paint Code

请注意,在 Paint Code 的底部面板中,现在有一个单一形状的代码,这就是我希望能够以编程方式获得大约 1000 个原始路径的代码。

最佳答案

您可以通过遵循 Core 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!
}
}

第一个路径:-

enter image description here

第二条路径:-

enter image description here

最终结果:-

enter image description here

引用:- Blend in Core Graphics , Creating Image

GitHub Demo

关于ios - 联合 UIBezierPath 而不是追加路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23497703/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com