I use image erase functionality and my code is as follows. You can see the video HERE . Here current_sticker_img
is myImageview
NOTE: I also use the Gesture function to scale the image.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if btn_eraser.isSelected == true || btn_repaint.isSelected == true{
let touch : UITouch = touches.first!
lastpoint = touch.location(in: current_sticker_img)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if btn_eraser.isSelected == true || btn_repaint.isSelected == true{
let touch = touches.first!
let currentPoint : CGPoint = touch.location(in: current_sticker_img)
let isPointInsideView = current_sticker_img.point(inside: currentPoint, with: event)
if isPointInsideView
{
UIGraphicsBeginImageContext(current_sticker_img.frame.size)
current_sticker_img
.image?.draw(in: CGRect(x: 0, y: 0, width: (current_sticker_img.frame.size.width), height: (current_sticker_img.frame.size.height)))
UIGraphicsGetCurrentContext()!.setLineCap(.round)
UIGraphicsGetCurrentContext()?.setLineWidth(Current_slider_value_for_Eraser)
UIGraphicsGetCurrentContext()?.setShadow(offset: CGSize(width: CGFloat(0), height: CGFloat(0)), blur: 0, color: UIColor.clear.cgColor )
UIGraphicsGetCurrentContext()!.setBlendMode(.clear)
UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: CGFloat(lastpoint.x), y: CGFloat(lastpoint.y)))
UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: CGFloat(currentPoint.x), y: CGFloat(currentPoint.y)))
UIGraphicsGetCurrentContext()!.strokePath()
current_sticker_img.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
lastpoint = currentPoint
}
}
}
What am I doing wrong? Please help me with this. thank you
source
share