Ios 如何在swift中在图像上自由绘制/涂鸦线条?
我已经搜索了这个案例的解决方案,但在互联网上没有找到。我想画/涂鸦一条线,比如snapchat或whatsapp状态,允许用户自由绘制。我发现了一个类似的教程,但它只是在本教程的UIView上绘制的: 我想允许用户自由地绘制他们从图像库/相机中拾取的图像,然后将结果保存回UIImage UIView上涂鸦的源代码(第一张图片)如下所示 首先,创建自定义视图Ios 如何在swift中在图像上自由绘制/涂鸦线条?,ios,swift,uiimageview,uiimage,Ios,Swift,Uiimageview,Uiimage,我已经搜索了这个案例的解决方案,但在互联网上没有找到。我想画/涂鸦一条线,比如snapchat或whatsapp状态,允许用户自由绘制。我发现了一个类似的教程,但它只是在本教程的UIView上绘制的: 我想允许用户自由地绘制他们从图像库/相机中拾取的图像,然后将结果保存回UIImage UIView上涂鸦的源代码(第一张图片)如下所示 首先,创建自定义视图 class DrawView : ShadowView { var isDrawing = false var las
class DrawView : ShadowView {
var isDrawing = false
var lastPoint : CGPoint!
var strokeColor : CGColor! = UIColor.black.cgColor
var strokes = [Stroke]()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard !isDrawing else { return }
isDrawing = true
guard let touch = touches.first else {return}
let currentPoint = touch.location(in: self)
lastPoint = currentPoint
print(currentPoint)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard isDrawing else { return}
guard let touch = touches.first else {return}
let currentPoint = touch.location(in: self)
let stroke = Stroke(startPoint: lastPoint, endPoint: currentPoint, strokeColor: strokeColor)
strokes.append(stroke)
lastPoint = currentPoint
setNeedsDisplay()
print(currentPoint)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
guard isDrawing else { return}
isDrawing = false
guard let touch = touches.first else {return}
let currentPoint = touch.location(in: self)
let stroke = Stroke(startPoint: lastPoint, endPoint: currentPoint, strokeColor: strokeColor)
strokes.append(stroke)
setNeedsDisplay()
lastPoint = nil
print(currentPoint)
}
override func draw(_ rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
context?.setLineWidth(5)
context?.setLineCap(.round)
for stroke in strokes {
context?.beginPath()
context?.move(to: stroke.startPoint)
context?.addLine(to: stroke.endPoint)
context?.setStrokeColor(stroke.strokeColor)
context?.strokePath()
}
}
func erase() {
strokes = []
strokeColor = UIColor.black.cgColor
setNeedsDisplay() // ditampilkan ke layar
}
}
之后,将自定义视图指定给ViewController:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var drawView: DrawView!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func yellowDidPressed(_ sender: Any) {
drawView.strokeColor = UIColor.yellow.cgColor
}
@IBAction func redDidPressed(_ sender: Any) {
drawView.strokeColor = UIColor.red.cgColor
}
@IBAction func blueDidPressed(_ sender: Any) {
drawView.strokeColor = UIColor.blue.cgColor
}
@IBAction func erasedDidPressed(_ sender: Any) {
drawView.erase()
}
}
那么,如果我想在UIImageView上应用该自定义视图并将结果保存为新的UIImage,该怎么办呢?我是编程新手,实际上,我真的需要你的帮助。谢谢:)同样的问题,有什么解决办法吗?
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var drawView: DrawView!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func yellowDidPressed(_ sender: Any) {
drawView.strokeColor = UIColor.yellow.cgColor
}
@IBAction func redDidPressed(_ sender: Any) {
drawView.strokeColor = UIColor.red.cgColor
}
@IBAction func blueDidPressed(_ sender: Any) {
drawView.strokeColor = UIColor.blue.cgColor
}
@IBAction func erasedDidPressed(_ sender: Any) {
drawView.erase()
}
}