gpt4 book ai didi

ios - 使用 Core Graphics 在 UIImageView 上绘制一个矩形

转载 作者:行者123 更新时间:2023-11-29 11:42:06 27 4
gpt4 key购买 nike

我想让用户在 UIImageView 上画一个矩形我为第一个和最后一个触摸位置添加了两个变量我添加了这个功能:-

func draw(from: CGPoint, to: CGPoint) {
UIGraphicsBeginImageContext(imageView.frame.size)
context = UIGraphicsGetCurrentContext()
context?.setStrokeColor(UIColor(red: 0, green: 0, blue: 0, alpha: 1.0).cgColor)
context?.setLineWidth(5.0)
let currentRect = CGRect(x: from.x,
y: from.y,
width: to.x - from.x,
height: to.y - from.y)
context?.addRect(currentRect)
context?.drawPath(using: .stroke)
context?.strokePath()
imageView.image?.draw(in: self.imageView.frame)
imageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}

我将方法添加到 (touchMoved) 它绘制了许多矩形

我将方法添加到 (touchEnded) 它绘制了一个,但是当用户移动触摸时它没有出现

screenshot

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
firstTouchLocation = touch.location(in: self.view)
}
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
lastTouchLocation = touch.location(in: self.view)
draw(from: firstTouchLocation, to: lastTouchLocation)
}
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
lastTouchLocation = touch.location(in: self.view)
draw(from: firstTouchLocation, to: lastTouchLocation)
}
}

我想让用户在 touchMoved 时扩展矩形并在 touchEnded 时绘制。

最佳答案

您正在将 image 替换为由先前的 image 加上在其上绘制的矩形组成的新图像。不要从 ImageView 中绘制图像,而是绘制原始图像。

或者,您可以将矩形渲染为形状图层,然后只更新该形状图层的路径:

class ViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!

private let shapeLayer: CAShapeLayer = {
let _shapeLayer = CAShapeLayer()
_shapeLayer.fillColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0).cgColor
_shapeLayer.strokeColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1).cgColor
_shapeLayer.lineWidth = 3
return _shapeLayer
}()

private var startPoint: CGPoint!

override func viewDidLoad() {
super.viewDidLoad()

imageView.layer.addSublayer(shapeLayer)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
startPoint = touches.first?.location(in: imageView)
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let startPoint = startPoint, let touch = touches.first else { return }

let point: CGPoint

if let predictedTouch = event?.predictedTouches(for: touch)?.last {
point = predictedTouch.location(in: imageView)
} else {
point = touch.location(in: imageView)
}

updatePath(from: startPoint, to: point)
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let startPoint = startPoint, let touch = touches.first else { return }

let point = touch.location(in: imageView)

updatePath(from: startPoint, to: point)
imageView.image = imageView.snapshot(afterScreenUpdates: true)
shapeLayer.path = nil
}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
shapeLayer.path = nil
}

private func updatePath(from startPoint: CGPoint, to point: CGPoint) {
let size = CGSize(width: point.x - startPoint.x, height: point.y - startPoint.y)
let rect = CGRect(origin: startPoint, size: size)
shapeLayer.path = UIBezierPath(rect: rect).cgPath
}

}

地点:

extension UIView {
func snapshot(afterScreenUpdates: Bool = false) -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, 0)
drawHierarchy(in: bounds, afterScreenUpdates: afterScreenUpdates)
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
}

这不仅更简单,而且效率更高。

产生:

enter image description here

顺便说一下,我可能会建议在 touchesMoved 中使用预测触摸。在可以产生稍微更灵敏的 UI 的设备(不是模拟器)上。

关于ios - 使用 Core Graphics 在 UIImageView 上绘制一个矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45906289/

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