我正在创建一个橡皮擦应用程序,其中我有一张图片,用户可以使用以下函数擦除背景:
func eraseImage( image: UIImage ,line: [CGPoint] ,brushSize: CGFloat) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(proxy.size, false, 0)
let context = UIGraphicsGetCurrentContext()
let rect = AVMakeRect(aspectRatio: image.size, insideRect: CGRect(x: 0, y:0, width: proxy.size.width, height: proxy!.size.height))
//lassoImageView.image?.draw(in: calculateRectOfImageInImageView(imageView: lassoImageView))
image.draw(in: rect, blendMode: .normal, alpha: 1)
context?.move(to: CGPoint(x: line.first!.x - 50, y: line.first!.y - 50))
for pointIndex in 1..<line.count {
context?.addLine(to: CGPoint(x: line[pointIndex].x - 50, y: line[pointIndex].y - 50))
}
context?.setBlendMode(.clear)
context?.setLineCap(.round)
context?.setLineWidth(brushSize)
context?.setShadow(offset: CGSize(width: 0, height: 0), blur: 8)
context?.strokePath()
if let img = UIGraphicsGetImageFromCurrentImageContext() {
return img
}
UIGraphicsEndImageContext()
return nil
}我正在努力弄清楚如何添加一个绘图功能,让用户可以纠正它的先锋错误,并拉回一些部分。我正在考虑在编辑后的图像上绘制原始图像,该图像带有跟踪用户位置的cgpoint的路径蒙版。这有可能吗?
发布于 2021-04-23 21:47:52
我建议使用第二个CALayer作为遮罩来设置您的图像。将图像视图安装到遮罩层的内容中,并使用透明/不透明颜色绘制到蒙版中,以遮罩/曝光图像层中的像素。
https://stackoverflow.com/questions/67230181
复制相似问题