gpt4 book ai didi

ios - 如何使用 Graphics Context 清除具有关闭路径的任何 imageView 的图像

转载 作者:行者123 更新时间:2023-11-28 08:20:26 26 4
gpt4 key购买 nike

当用户在屏幕上拖动触摸时,我正在使用此代码清除图像的一部分。我有多个图像相互覆盖。现在清楚到最顶层工作正常,所以下部图像的一部分是可见的。现在我想要实现的是用户选择一条关闭路径,并且所选关闭路径的区域应该是清晰的。 用户可以选择多个图层并选择要剪切的任何部分。例如,如果有 8 张图像并且用户选择了第 6 层到第 8 层,那么可见部分将来自第 5 层,用户可以通过触摸清除该层。

func drawBrushOnLayer(fromPoint: CGPoint, toPoint: CGPoint , selected:[Int]) {               

UIGraphicsBeginImageContext(DrawImage.frame.size)

var context = UIGraphicsGetCurrentContext()
DrawImage.image?.draw(in: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height - 50))

context?.move(to: fromPoint)

context?.addLine(to: toPoint)

context?.setLineCap(.butt)

context?.setLineWidth(BrushSize)

context?.setBlendMode(.clear)

context?.setShouldAntialias(false)

UIColor.clear.set()

context?.strokePath()

DrawImage.image = UIGraphicsGetImageFromCurrentImageContext()!

UIGraphicsEndImageContext()

}

现在我在图像上绘制白色,但我需要清除选择的区域,以便可以看到较低的图像。

 func drawFill(point : CGPoint) {

autoreleasepool{

UIGraphicsBeginImageContext(CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height - 50))

DrawImage .draw(in: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height - 50))

UIColor.white.set()

BezierPath.addLine(to: point)

BezierPath.lineWidth = 2.0

BezierPath .close()

BezierPath.fill()

let context = UIGraphicsGetCurrentContext()

context?.addPath(lassoBezier.cgPath)

newImage = UIGraphicsGetImageFromCurrentImageContext()!

DrawImage.image = newImage

UIGraphicsEndImageContext()

}

}

最佳答案

func drawFill(point : CGPoint) {

    autoreleasepool{

UIGraphicsBeginImageContext(CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height - 50))

DrawImage .draw(in: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height - 50))

BezierPath.addLine(to: point)

BezierPath .close()

let context = UIGraphicsGetCurrentContext()
UIColor.clear.set()
context?.addPath(lassoBezier.cgPath)
context?.setLineCap(.square)
context?.setBlendMode(.clear)
context?.setShouldAntialias(false)
context?.fillPath()
newImage = UIGraphicsGetImageFromCurrentImageContext()!

DrawImage.image = newImage

UIGraphicsEndImageContext()

}

关于ios - 如何使用 Graphics Context 清除具有关闭路径的任何 imageView 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41484297/

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