【问题标题】:Drawing App in swift, slow performance & low FPS快速、缓慢的性能和低 FPS 的绘图应用程序
【发布时间】:2017-07-20 12:37:36
【问题描述】:

我正在尝试创建一个绘图应用程序,使用此代码绘制线条,但性能非常慢

 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
}

【问题讨论】:

  • 你想达到什么目的?这个方法被调用了多少次,什么时候?是每次触摸吗?只是一次吗?在这里你实际上是在创造和形象,这就是你想要的吗?请提供更多上下文,以及在什么设备上运行缓慢,参数是什么......等等

标签: ios swift performance drawing frame-rate


【解决方案1】:

绘制过多线条时性能会变慢。根据绘图区域的大小以及像素在显示比例上表示的距离,考虑通过不绘制所有线条来优化性能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    • 2016-09-11
    • 1970-01-01
    相关资源
    最近更新 更多