I am creating a demo IOS application that will be a simple drawing app. I have used Quartz 2D to create a basic app. However, the lines that is draws are very jagged. Im looking for a way to apply anti-aliasing or blending to make the lines smooth. The drawLineFrom looks like this:
func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {
UIGraphicsBeginImageContext(view.frame.size)
let context = UIGraphicsGetCurrentContext()
tempImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))
CGContextMoveToPoint(context, fromPoint.x, fromPoint.y)
CGContextAddLineToPoint(context, toPoint.x, toPoint.y)
CGContextSetAllowsAntialiasing(context, true)
CGContextSetShouldAntialias(context, true)
CGContextSetLineCap(context, kCGLineCapRound)
CGContextSetLineWidth(context, brushWidth)
CGContextSetRGBStrokeColor(context, red, green, blue, 1.0)
CGContextSetBlendMode(context, kCGBlendModeNormal)
CGContextStrokePath(context)
tempImageView.image = UIGraphicsGetImageFromCurrentImageContext()
tempImageView.alpha = opacity
UIGraphicsEndImageContext()
}
The project can be found here: https://github.com/BananaSplitio/OpenSketch
Thanks for the help!
I know I'm massively late to the party here but if you changed:
UIGraphicsBeginImageContext(view.frame.size)
to
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0f)
;
that should solve the jagged line issue. The last argument 0.0f
specifies the screen scale. Setting this to 0.0f
will automatically set it to the resolution of the device. Without this it draws it with 1 point = 1 pixel (so non retina), causing things to look jagged.