0

I'm working on a simple freehand drawing app. Below is the sample code, I'm using the UIGesture class to track the scribble, i.e., pencil or finger movement, and then stroking those touch points on the UIImageView

    let previousLocation = touch.previousLocation(in: self)
    let location = touch.location(in: self)
    var lineWidth: CGFloat = 3
    UIColor.black.setStroke()
    context?.setLineWidth(lineWidth)
    context?.setLineCap(.round)
    context?.move(to: previousLocation)
    context?.addLine(to: location)
    context?.strokePath() 

the strokes are blurry on the edges and the result looks like this:

enter image description here

Few other things I have tried: 1)Stroking bezier pathh 2)Using view instead of image view 3)Increasing scale here -> UIGraphicsBeginImageContextWithOptions, this does increase the sharpness but the overall writing lags

How do I get sharp annotations?

Raj Kiran
  • 152
  • 1
  • 9
  • 1
    This looks like typical softness when upscaling an image. And yes, setting scale to 0 (or using `UIGraphicsImageRenderer`) should address the softness. And you noted that setting scale solved the sharpness problem, but suggest that it is laggy (which makes sense, has 4 or 9 times as much data!). Seems like you have the answer to “how to make it sharp” and the real question should be “how to reduce lagginess”. I’d suggest deleting this question and posting a question with reproducible code example of lagginess. It probably is issue of where you are snapshotting or frequency of those snapshots. – Rob Feb 05 '23 at 19:24
  • 1
    Unrelated aside: I’d suggest avoiding JPG in questions like this. It is hard to disambiguate JPG artifacts from rendering issues. E.g., I’ve [blown this up](https://i.stack.imgur.com/5yiHS.png) where I can see individual pixels, and it is not entirely clear whether we are seeing JPG artifacts, or some upscaled rendition of “jaggies” common in a non-anti-aliased stroke. Probably the latter, but we shouldn’t have to guess. I’d suggest always using lossless compression (like PNG) for questions like this. – Rob Feb 05 '23 at 19:25
  • @Rob I have created a separate question about the lag issue, it has the sample class that I'm working on, please check it out. Thanks. https://stackoverflow.com/q/75365742/9855378 – Raj Kiran Feb 06 '23 at 19:30

0 Answers0