title: Trying to make erase uiimage with swift
body: <p>I'm trying to make eraser feature(and restore in future) for my app but facing some problems. I'm using Swift 4.2 and this what I made so far:</p>
  
  <pre><code>override func touchesBegan(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
          if let touch = touches.first as UITouch?{
              let touchPoint = touch.location(in: self.lassoImageView) + lassoOffset!
              print("touch begin to : \(touchPoint)")
              eraserStartPoint = touchPoint   
          }
      }
  </code></pre>
  
  <pre><code>override func touchesMoved(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
          if let touch = touches.first as UITouch?{
              let touchPoint = touch.location(in: self.lassoImageView) + lassoOffset!
              print("touch moved to : \(touchPoint)")
              erase(fromPoint: eraserStartPoint!, toPoint: touchPoint)
              eraserStartPoint = touchPoint
          }
      }
  </code></pre>
  
  <pre><code>override func touchesEnded(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
          if let touch = touches.first as UITouch?{
              let touchPoint = touch.location(in: self.lassoImageView) + lassoOffset!
              print("touch ended at : \(touchPoint)")
              erase(fromPoint: touchPoint, toPoint: touchPoint)
              UIGraphicsBeginImageContext(lassoImageView.frame.size)
                  lassoImageView.image?.draw(in: CGRect(x: 0, y: 0, width: lassoImageView.frame.size.width, height: lassoImageView.frame.size.height), blendMode: .normal, alpha: 1.0)
                  lassoImageView.image?.draw(in:  CGRect(x: 0, y: 0, width: lassoImageView.frame.size.width, height: lassoImageView.frame.size.height), blendMode: .normal, alpha: 1.0)
                  lassoImageView.image = UIGraphicsGetImageFromCurrentImageContext()
                  UIGraphicsEndImageContext()   
      }
  </code></pre>
  
  <pre><code>func erase(fromPoint: CGPoint, toPoint: CGPoint) {
          UIGraphicsBeginImageContextWithOptions(lassoImageView.bounds.size, false, 1)
          //UIGraphicsBeginImageContext(lassoImageView.bounds.size)
          //UIGraphicsBeginImageContext(lassoImageView.image!.size)
          let context = UIGraphicsGetCurrentContext()
          lassoImageView.image?.draw(in: CGRect(x: 0, y: 0, width: lassoImageView.frame.size.width, height: lassoImageView.frame.size.height))
          //lassoImageView.image?.draw(in: calculateRectOfImageInImageView(imageView: lassoImageView))
  
          context?.move(to: fromPoint)
          context?.addLine(to: toPoint)
  
          context?.setLineCap(.round)
          context?.setLineWidth(CGFloat(eraserBrushWidth))
  
          context?.setBlendMode(.clear)
  
          context?.strokePath()
  
          lassoImageView.image = UIGraphicsGetImageFromCurrentImageContext()
          croppedImage = UIGraphicsGetImageFromCurrentImageContext()
  
          UIGraphicsEndImageContext()
      }
  </code></pre>
  
  <pre><code>lassoImageView // this is UIImageView where I'm loading image from gallery and then erasing image with my finger
  
  lassoOffset // this is Float for touch offset because finger hides part of image
  </code></pre>
  
  <p>Code is simple and it actually works but if image aspect ration is different from UIImageView aspect ratio. UIImageView content mode is set to <code>Aspect Fit</code> but anyways when I drag my finger image become stretched like if it was <code>Scale to Fill</code></p>
  
  <p>I believe problem is with my rect in this line:</p>
  
  <pre><code>lassoImageView.image?.draw(in: CGRect(x: 0, y: 0, width: lassoImageView.frame.size.width, height: lassoImageView.frame.size.height))
  </code></pre>
  
  <p>I even tried to calculate image rect itself but in this case image just disappeared after <code>touches begun</code></p>
  
  <p>Maybe my question is noob and I probably missing something ridiculous, but I really really need to figure this out.</p>
  
  <p>Thanks to every one and have a great day</p>
  
  <p>Best, Victor</p>