|
@@ -21,7 +21,7 @@ class TSAIRemovePhotlVC: TSAIUploadPhotoVC {
|
|
|
drawingLayer.fillColor = UIColor.clear.cgColor
|
|
|
drawingLayer.lineCap = .round
|
|
|
drawingLayer.lineJoin = .round
|
|
|
- drawingLayer.lineWidth = brushWidth
|
|
|
+ drawingLayer.lineWidth = effectiveBrushWidth
|
|
|
drawingLayer.strokeColor = brushColor.cgColor
|
|
|
return drawingLayer
|
|
|
|
|
@@ -116,7 +116,20 @@ class TSAIRemovePhotlVC: TSAIUploadPhotoVC {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ // 计算属性:根据当前缩放级别返回适当笔触粗细
|
|
|
+ private var effectiveBrushWidth: CGFloat {
|
|
|
+// let baseWidth: CGFloat = 5.0 // 基础宽度(zoomScale=1时的宽度)
|
|
|
+// let minWidth: CGFloat = 1.0 // 最小宽度(防止缩放过大时笔触消失)
|
|
|
+// let maxWidth: CGFloat = 100.0 // 最大宽度(防止缩放过小时笔触过粗)
|
|
|
+//
|
|
|
+// // 计算缩放后的宽度(与缩放比例成反比)
|
|
|
+// let scaledWidth = baseWidth / scrollView.zoomScale
|
|
|
+// // 限制在合理范围内
|
|
|
+// return min(max(scaledWidth, minWidth), maxWidth)
|
|
|
+
|
|
|
+ return brushWidth / scrollView.zoomScale
|
|
|
+ }
|
|
|
+
|
|
|
override func setUpStackView() {
|
|
|
setUpRemoveUploadView()
|
|
|
setupUI()
|
|
@@ -238,7 +251,6 @@ class TSAIRemovePhotlVC: TSAIUploadPhotoVC {
|
|
|
scrollView.addGestureRecognizer(pinchGesture)
|
|
|
|
|
|
// 绘图手势
|
|
|
-// let panGesture = ImmediatePanGestureRecognizer(target: self, action: #selector(handleDrawingPan(_:)))
|
|
|
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handleDrawingPan(_:)))
|
|
|
panGesture.minimumNumberOfTouches = 1
|
|
|
panGesture.maximumNumberOfTouches = 1
|
|
@@ -279,6 +291,7 @@ class TSAIRemovePhotlVC: TSAIUploadPhotoVC {
|
|
|
case .began:
|
|
|
currentPath = UIBezierPath()
|
|
|
currentPath.lineCapStyle = .round
|
|
|
+ currentPath.lineWidth = effectiveBrushWidth
|
|
|
currentPath.lineJoinStyle = .round
|
|
|
currentPath.move(to: currentPoint)
|
|
|
case .changed:
|
|
@@ -336,7 +349,7 @@ class TSAIRemovePhotlVC: TSAIUploadPhotoVC {
|
|
|
brushWidth = CGFloat(sender.value)
|
|
|
// panLineLabel.text = "\(brushWidth)"
|
|
|
panLineLabel.text = "\(Int(brushWidth))"
|
|
|
- drawingLayer.lineWidth = brushWidth
|
|
|
+ drawingLayer.lineWidth = effectiveBrushWidth //brushWidth
|
|
|
|
|
|
panRoundView.isHidden = false
|
|
|
panRoundView.cornerRadius = brushWidth/2.0
|
|
@@ -432,108 +445,9 @@ class TSAIRemovePhotlVC: TSAIUploadPhotoVC {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
-// 自定义立即响应的手势识别器
|
|
|
-class ImmediatePanGestureRecognizer: UIPanGestureRecognizer {
|
|
|
-// override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
|
|
|
-// super.touchesBegan(touches, with: event)
|
|
|
-// self.state = .began
|
|
|
-// }
|
|
|
-
|
|
|
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
|
|
|
- // 只有在单指触摸时才立即开始
|
|
|
- if touches.count == 1 {
|
|
|
- super.touchesBegan(touches, with: event)
|
|
|
- self.state = .began
|
|
|
- } else {
|
|
|
- // 多指触摸时不处理
|
|
|
- super.touchesBegan(touches, with: event)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
|
|
|
- // 确保仍然是单指操作
|
|
|
- if touches.count == 1 {
|
|
|
- super.touchesMoved(touches, with: event)
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
|
|
|
extension TSAIRemovePhotlVC {
|
|
|
|
|
|
- func getBlackWhiteMaskImage1() -> UIImage? {
|
|
|
- // 确保有原始图片
|
|
|
- guard let originalImage = uploadImageView.image else {
|
|
|
- print("没有可处理的图片")
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- // 开始图像处理
|
|
|
- UIGraphicsBeginImageContextWithOptions(originalImage.size, true, originalImage.scale)
|
|
|
- defer { UIGraphicsEndImageContext() }
|
|
|
-
|
|
|
- // 获取绘图上下文
|
|
|
- guard let context = UIGraphicsGetCurrentContext() else {
|
|
|
- print("无法创建图像上下文")
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- // 1. 绘制全黑背景
|
|
|
- UIColor.black.setFill()
|
|
|
- context.fill(CGRect(origin: .zero, size: originalImage.size))
|
|
|
-
|
|
|
- // 2. 绘制白色涂鸦(从drawingImageView获取)
|
|
|
- if let drawingImage = drawingImageView.image {
|
|
|
- // 使用白色混合模式绘制涂鸦
|
|
|
- context.saveGState()
|
|
|
- context.setBlendMode(.normal)
|
|
|
- context.interpolationQuality = .high
|
|
|
-
|
|
|
- // 将涂鸦转换为白色
|
|
|
- let tintedImage = drawingImage.tinted(with: .white)
|
|
|
- tintedImage.draw(in: CGRect(origin: .zero, size: originalImage.size))
|
|
|
- context.restoreGState()
|
|
|
- }
|
|
|
-
|
|
|
- // 3. 获取处理后的图像
|
|
|
- guard let processedImage = UIGraphicsGetImageFromCurrentImageContext() else {
|
|
|
- print("无法生成处理后的图像")
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- return processedImage
|
|
|
- // UIImageWriteToSavedPhotosAlbum(processedImage, nil, nil, nil)
|
|
|
- // let alert = UIAlertController(title: "Image Generated", message: "The combined image has been saved to your photos.", preferredStyle: .alert)
|
|
|
- // alert.addAction(UIAlertAction(title: "OK", style: .default))
|
|
|
- // present(alert, animated: true)
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- func getBlackWhiteMaskImage2() -> UIImage? {
|
|
|
- UIGraphicsBeginImageContextWithOptions(drawingImageView.bounds.size, false, 1.0)
|
|
|
- defer { UIGraphicsEndImageContext() }
|
|
|
-
|
|
|
- guard let context = UIGraphicsGetCurrentContext() else { return nil}
|
|
|
-
|
|
|
- for layer in pathsHistory {
|
|
|
- if let path = layer.path {
|
|
|
- context.addPath(path)
|
|
|
- context.setLineWidth(layer.lineWidth)
|
|
|
- context.setLineCap(CGLineCap(layer.lineCap))
|
|
|
- context.setLineJoin(CGLineJoin(layer.lineJoin))
|
|
|
- context.setStrokeColor(layer.strokeColor ?? UIColor.black.cgColor)
|
|
|
- context.strokePath()
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if let image = UIGraphicsGetImageFromCurrentImageContext() {
|
|
|
- UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
|
|
|
- return image
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
func getBlackWhiteMaskImage() -> UIImage? {
|
|
|
// 1. 安全检查
|
|
|
guard let originalImage = uploadImageView.image,
|
|
@@ -663,6 +577,7 @@ extension TSAIRemovePhotlVC:UIScrollViewDelegate {
|
|
|
func scrollViewDidZoom(_ scrollView: UIScrollView) {
|
|
|
// 缩放后调整图片位置
|
|
|
adjustImagePosition()
|
|
|
+ drawingLayer.lineWidth = effectiveBrushWidth
|
|
|
}
|
|
|
|
|
|
private func adjustImagePosition() {
|
|
@@ -671,7 +586,6 @@ extension TSAIRemovePhotlVC:UIScrollViewDelegate {
|
|
|
|
|
|
let verticalPadding = max((scrollViewSize.height - imageViewSize.height) / 2, 0)
|
|
|
let horizontalPadding = max((scrollViewSize.width - imageViewSize.width) / 2, 0)
|
|
|
- print("verticalPadding=\(verticalPadding),horizontalPadding=\(horizontalPadding)")
|
|
|
scrollView.contentInset = UIEdgeInsets(
|
|
|
top: verticalPadding,
|
|
|
left: horizontalPadding,
|