Post

Replies

Boosts

Views

Activity

UIView draw always draws the whole frame
I am trying to build a drawing app for pencil with features like layers, importing photos, and undo redo, and initial tests of the responsiveness of the drawing engine are not so great. I'm experimenting based on the SpeedSketch example, except using a bitmap CGContext as a back-buffer to store a layer's pixels. I figure I have to use rasters because of the importing images feature. For the same reason I don't think I can use PencilKit. I've done some debugging and it seems that maybe the sluggishness is because every time draw is called in the drawing view, the whole frame is updated. I've overridden the setNeedsDisplay functions to try and see which rects are invalidated and this part seems fine. For whatever reason UIKit is always calling draw with a full frame. here is the code in my view: override func setNeedsDisplay(_ rect : CGRect) {         print("Needs display \(rect)")         super.setNeedsDisplay(rect)     }     override func setNeedsDisplay() {         print("Called set needs display for whole frame")         super.setNeedsDisplay()     }     override func draw(_ rect: CGRect) {         print("Draw rectangle \(rect) in bounds \(bounds.size)")                  guard let context = UIGraphicsGetCurrentContext() else {             print("Failed to get context")             return         }         if bitmapGraphicsContext != nil {             if (cachedImage == nil ) {                 cachedImage = bitmapGraphicsContext!.makeImage()             }             let transformedRect = transformRect(rect)             let image = cachedImage?.cropping(to: transformedRect)             if let imageRef = image {                 context.draw(imageRef, in: rect)             }         }         context.setBlendMode(CGBlendMode.clear)         if let stroke = strokeToDraw {             draw(stroke: stroke, in: rect)         }     } the output is: Called set needs display for whole frame Draw rectangle (0.0, -0.17777, 557.5, 248.0) in bounds (557.6, 247.822) Needs display (177.8, 142.91, 40.0, 40.0) Draw rectangle (0.0, -0.17777, 557.5, 248.0) in bounds (557.6, 247.822) Would love some help if anyone knows what to do
2
0
909
Jun ’22