0

I've got a UIView. In function drawRect() I'm drawing a grid on it (vertical and horizontal lines). When I run my app on iPhone 4S - fps is not high enough. Without interface everything is pretty good, but if there is something else on the screen - fps falling down.

I optimized drawing function as much as I could. For example I'm not using CGContextAddLineToPoint(). Instead of it I'm drawing lines using CGContextStrokeRect(), because as I could see - the last one works faster.

Anyway - app still works not fast enough. iPhone 4S and new models have few graphics cores, so I think, that I can speed up my app by drawing grid in another thread

I tried to make it like that

override func drawRect(rect: CGRect) {
    let context = UIGraphicsGetCurrentContext()
    let screenScale = self.window!.screen.scale
    let queue = NSOperationQueue()

    queue.addOperationWithBlock() {
        drawGrid(context, rect: rect, screenScale: screenScale)
    }
}

But for some reason drawn grid is twice smaller then must to be.

So what am I doing wrong and can I draw on UIView in separate thread using Swift? Please, help me to find out.

Yury Imashev
  • 2,068
  • 1
  • 18
  • 32

1 Answers1

0

Consider using a png image instead of manually drawing everything. Get more information here.

Customizing... with a UIImage background relies entirely on the GPU for rendering the image assets saved on disk.

...

The drawRect method relies on Core Graphics to do the custom drawing, but its main drawback lies in the way it handles touch events: each time the button is pressed, setNeedsDisplay forces it to redraw; not only once, but twice for every single tap. This is not a good use of CPU and memory...

Daniel T.
  • 32,821
  • 6
  • 50
  • 72