如何使用图形上下文清除任何图像的图像具有接近路径的视图



当用户在屏幕上拖动触摸时,我正在使用此代码清除图像的一部分。我有多个图像相互覆盖。现在清除到顶部最层工作正常,因此可以看到下部图像的一部分。现在我想要实现的是用户选择一个关闭路径,并且所选关闭路径的区域应该清晰。用户可以选择多个图层并选择要切割的任何部分。例如,如果有 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()
    }

}

最新更新