How to remove antialiasing in CGContext?

I'm trying to create a brush by drawing in CGContext using a UIImage for a brush. However, I when I try drawing, the stroke is antialiased when I don't want it to be. I tried

context.interpolationQuality = .none
context.setShouldAntialias(false)

but it doesn't seem to work. Is it a problem with my brush or resizing the brush maybe?? (Also this problem doesn't occur when I draw a regular stroke without the brush.)

Any help or advice would be greatly appreciated!

Here is my draw function

override func draw(_ rect: CGRect) {
    super.draw(rect)

    guard let context = UIGraphicsGetCurrentContext() else { return }
    
    context.interpolationQuality = .none
    context.setShouldAntialias(false)
    
    for stroke in strokes {
        let brush = UIColor.blue.circle(size: CGSize(width: CGFloat(stroke.width * 10), height: CGFloat(stroke.width * 10))).mask(color: UIColor(cgColor: stroke.color))
        
        var first = true
        for point in stroke.points {
            if first {
                first = false
                context.move(to: point)
                continue
            }
            context.addLine(to: point, using: brush)
        }
    }
}

Circle brush

extension UIColor {
    func circle(size: CGSize = CGSize(width: 1, height: 1)) -> UIImage {
        return UIGraphicsImageRenderer(size: size).image { rendererContext in
            self.setFill()
            UIBezierPath(ovalIn: CGRect(origin: .zero, size: size)).fill()
        }
    }
}

CGConext extension where I overloaded addLine

extension CGContext {
    func addLine(to point: CGPoint, using brush: UIImage, density: CGFloat = 1.0) {
        var frame: CGRect = .zero
        frame.size = brush.size
        
        let lastPoint = self.currentPointOfPath
        
        let distanceX = point.x - lastPoint.x
        let distanceY = point.y - lastPoint.y
        let distanceR = sqrt(pow(distanceX, 2) + pow(distanceY, 2))
        
        let deltaR = (1.0 / density)
        let numOfSteps = ceil(distanceR / deltaR)
        var renders : CGFloat = 0.0
        
        let deltaX = distanceX / numOfSteps
        let deltaY = distanceY / numOfSteps
        
        var currentCenter = lastPoint
        repeat {
            frame.origin.x = currentCenter.x - frame.width / 2.0
            frame.origin.y = currentCenter.y - frame.height / 2.0

            brush.draw(in: frame)
            currentCenter.x += deltaX
            currentCenter.y += deltaY
            renders += 1.0
        } while (renders <= numOfSteps)
        
        self.move(to: point)
    }
}

When you say "the stroke is antialiased" what do you mean exactly? In this case, the "strokes" are images drawn along the paths you provide to your CGContext addLine() method. Looking at your algorithm, when you're drawing images along the path they're not always going to be pixel aligned. Is the interpolation effect you're seeing with drawing those images getting confused with the antialiasing effect you're trying to turn off? Or are you expecting those to be more pixellated at every point along the line (including positions that are not pixel-aligned)?

@DTS Engineer Ohhh I see, that would explain the antialiasing effect. I would like to know if there is a way to make the stroke pixellated at every point along the line though, so that every pixel is either fully colored or not colored?

CGContexts support drawing to fractional virtual pixel positions. You might be able to avoid the pixelation by aligning your drawing with pixel widths. For a quick test, try rounding the coordinates you're using for placing your repeated images. That should align all of your images on virtual pixel boundaries. If that looks better, then maybe it might be worth investing the time in figuring out how to align with actual pixel values using the scaling information from UIScreen and doing some testing with that.

@DTS Engineer Ah okay, rounding did improve it, but didn't get rid of it completely. I'll definitely look into aligning the pixel values though, ty!

How to remove antialiasing in CGContext?
 
 
Q