Anmol Madaan
Anmol Madaan

Reputation: 11

Drawing App in swift, slow performance & low FPS

I am trying to create a drawing app , using this code to draw lines but it is giving a very slow performance

 func drawLineNew(_ prevPoint1 : CGPoint,_ prevPoint2 : CGPoint ,_ currentPoint : CGPoint){
    UIGraphicsBeginImageContextWithOptions(ImageView.frame.size, false, 0.0)
    let context = UIGraphicsGetCurrentContext()
    ImageView.image?.draw(in: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))
    var mid1 = CGPoint.init(x: (prevPoint1.x + prevPoint2.x)*0.5, y: (prevPoint1.y + prevPoint2.y)*0.5)
    var mid2 = CGPoint.init(x: ((currentPoint.x) + prevPoint1.x)*0.5, y: ((currentPoint.y) + prevPoint1.y)*0.5)
    context?.move(to: prevPoint2)
    context?.addCurve(to: mid2, control1: mid1, control2: prevPoint1)
    context?.setLineCap(CGLineCap.round)
    context?.setLineWidth(brushWidth)
    context?.setStrokeColor(red: red, green: green, blue: blue, alpha: opacity)
    context?.setBlendMode(CGBlendMode.normal )
    //context?.setShouldAntialias(true)
    context?.strokePath()
    ImageView.image = UIGraphicsGetImageFromCurrentImageContext()
    ImageView.alpha = 1.0
    self.prevPoint1 = mid2
    //context?.clear(CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: ImageView.frame.size))
    UIGraphicsEndImageContext()
    lastPoint = currentPoint
}

Upvotes: 1

Views: 340

Answers (1)

Karsten
Karsten

Reputation: 1929

The performance is slow when you draw too much lines. Consider optimizing the performance by not drawing all lines depending on the size of your drawing area and which distance a pixel is representing on the displayed scale.

Upvotes: 0

Related Questions