0
私は消しゴムツールを作成しようとしていると私はこれを書いた:消しゴムツール
class EraserTool: Tool {
var context: CGContext?
fileprivate unowned let delegate: ToolDelegate
fileprivate unowned let imageView: UIImageView
fileprivate var currentPoint = CGPoint()
required init(imageView: UIImageView, delegate: ToolDelegate) {
self.delegate = delegate
self.imageView = imageView
}
func toucheBegin(at point: CGPoint) {
guard let image = imageView.image else {
printErr("can't erase, image nil")
return
}
UIGraphicsBeginImageContext(image.size)
guard let context = UIGraphicsGetCurrentContext() else {
printErr("can't get current context")
return
}
context.setBlendMode(.clear)
context.setLineCap(.round)
context.setLineWidth(CGFloat(delegate.toolWidthPercent)/imageView.imageScale * 100)
self.context = context
delegate.saveImageBeforeEditing()
currentPoint = point
}
func toucheMoved(to point: CGPoint) {
guard let context = context else {
printErr("no context")
return
}
imageView.erase(from: currentPoint, to: point, context: context)
currentPoint = point
}
func toucheEnd(at point: CGPoint) {
guard let context = context else {return}
imageView.erase(from: currentPoint, to: point, context: context)
UIGraphicsEndImageContext()
}
}
extension UIImageView {
func erase(from startPoint: CGPoint, to endPoint: CGPoint, context: CGContext) {
guard let image = image else {
printErr("can't erase, image nil")
return
}
image.draw(at: CGPoint())
context.beginPath()
context.move(to: startPoint)
context.addLine(to: endPoint)
context.strokePath()
self.image = UIGraphicsGetImageFromCurrentImageContext()
}
}
それは小さな画像のため正常に動作しますが、私は大きな絵のためにそれを実行したとき - それは非常に遅いプロセスを。私は何か間違っているのですか?効率を改善する方法はいくつかありますか?