How to get a pixel precise image from CGPoints?

I'm trying to make some kind of pencil signature view in my app, I did it with the help of this func:


 func drawLine(from fromPoint: CGPoint, to toPoint: CGPoint) {
           UIGraphicsBeginImageContext(canvasImageView.frame.size)
           guard let context = UIGraphicsGetCurrentContext() else {
             return
           }
           canvasImageView.image?.draw(in: canvasImageView.bounds)
           
           context.move(to: fromPoint)
           context.addLine(to: toPoint)
           
           context.setLineCap(.round)
           context.setBlendMode(.normal)
           context.setLineWidth(brushWidth)
           context.setStrokeColor(color.cgColor)
           
           context.strokePath()
           
           canvasImageView.image = UIGraphicsGetImageFromCurrentImageContext()
           canvasImageView.alpha = opacity
           
           UIGraphicsEndImageContext()
         }

and I'm keeping track of the actual touches and draw the lines with those functions:

 open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
       guard let touch = touches.first else {
         return
       }
       swiped = false
       lastPoint = touch.location(in: canvasImageView)
       lines.append(lastPoint)
     }
  open override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
       guard let touch = touches.first else {
         return
       }
       swiped = true
       let currentPoint = touch.location(in: canvasImageView)
       drawLine(from: lastPoint, to: currentPoint)
         lines.append(currentPoint)
       lastPoint = currentPoint
     }
  open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
       if !swiped {
         drawLine(from: lastPoint, to: lastPoint)
       }
         lines.append(lastPoint)

       UIGraphicsBeginImageContext(canvasImageView.frame.size)
         canvasImageView?.image?.draw(in: canvasImageView.bounds, blendMode: .normal, alpha: opacity)
         canvasImageView.image = UIGraphicsGetImageFromCurrentImageContext()
       UIGraphicsEndImageContext()
     }

What happens now is that I'm able to draw on the canvas image view and export a base64 representation of the image, The problem is that the exported base64 includes the transparent image view and the signature but I need only the signature, Which means that I need the exported image size to represent only the drawn signature meaning that the pixels will differ with every different signature, now it's fixed and it equals the dimensions of the canvas image view, How I can achieve something like that?

So your question is how to get an image of just the signature, and not what's behind it?

Yes, exactly!

Well, the title of your post is very bizarre.

Before you capture the graphics context, make everything in your view except for the signature disappear.

Oh sorry for this, I was thinking that I need to use the drawn path CGPoints to export the image with the exact pixels size, also I feel that i don't get what you mean, you mean that I need to set the image of the canvas image view to nil before capturing the context?

Yes. Do that.