Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  2. if let touch = touches.first as UITouch?{
  3. let touchPoint = touch.location(in: self.lassoImageView) + lassoOffset!
  4. print("touch begin to : (touchPoint)")
  5. eraserStartPoint = touchPoint
  6. }
  7. }
  8.  
  9. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
  10. if let touch = touches.first as UITouch?{
  11. let touchPoint = touch.location(in: self.lassoImageView) + lassoOffset!
  12. print("touch moved to : (touchPoint)")
  13. erase(fromPoint: eraserStartPoint!, toPoint: touchPoint)
  14. eraserStartPoint = touchPoint
  15. }
  16. }
  17.  
  18. override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
  19. if let touch = touches.first as UITouch?{
  20. let touchPoint = touch.location(in: self.lassoImageView) + lassoOffset!
  21. print("touch ended at : (touchPoint)")
  22. erase(fromPoint: touchPoint, toPoint: touchPoint)
  23. UIGraphicsBeginImageContext(lassoImageView.frame.size)
  24. lassoImageView.image?.draw(in: CGRect(x: 0, y: 0, width: lassoImageView.frame.size.width, height: lassoImageView.frame.size.height), blendMode: .normal, alpha: 1.0)
  25. lassoImageView.image?.draw(in: CGRect(x: 0, y: 0, width: lassoImageView.frame.size.width, height: lassoImageView.frame.size.height), blendMode: .normal, alpha: 1.0)
  26. lassoImageView.image = UIGraphicsGetImageFromCurrentImageContext()
  27. UIGraphicsEndImageContext()
  28. }
  29.  
  30. func erase(fromPoint: CGPoint, toPoint: CGPoint) {
  31. UIGraphicsBeginImageContextWithOptions(lassoImageView.bounds.size, false, 1)
  32. //UIGraphicsBeginImageContext(lassoImageView.bounds.size)
  33. //UIGraphicsBeginImageContext(lassoImageView.image!.size)
  34. let context = UIGraphicsGetCurrentContext()
  35. lassoImageView.image?.draw(in: CGRect(x: 0, y: 0, width: lassoImageView.frame.size.width, height: lassoImageView.frame.size.height))
  36. //lassoImageView.image?.draw(in: calculateRectOfImageInImageView(imageView: lassoImageView))
  37.  
  38. context?.move(to: fromPoint)
  39. context?.addLine(to: toPoint)
  40.  
  41. context?.setLineCap(.round)
  42. context?.setLineWidth(CGFloat(eraserBrushWidth))
  43.  
  44. context?.setBlendMode(.clear)
  45.  
  46. context?.strokePath()
  47.  
  48. lassoImageView.image = UIGraphicsGetImageFromCurrentImageContext()
  49. croppedImage = UIGraphicsGetImageFromCurrentImageContext()
  50.  
  51. UIGraphicsEndImageContext()
  52. }
  53.  
  54. lassoImageView // this is UIImageView where I'm loading image from gallery and then erasing image with my finger
  55.  
  56. lassoOffset // this is Float for touch offset because finger hides part of image
  57.  
  58. lassoImageView.image?.draw(in: CGRect(x: 0, y: 0, width: lassoImageView.frame.size.width, height: lassoImageView.frame.size.height))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement