3 ms·
I wonder how to memoize stroke in Swift
I recently ventured into utilizing Swift's UIKit to create a custom PencilKit.
My approach involves sampling information about points every time the pencil touches the screen, storing this data in a format like
strokeCollection = {strokes: [Stroke], drawingStroke: Stroke?}
and overriding the UIView's draw method to redraw the view each time the strokeCollection is updated. However, as more strokes are drawn, the app begins to significantly slow down due to the need to redraw every stroke continually.
In React Native Skia, I've used the "createPicture" method to leverage the "Picture" component, keeping previously drawn strokes intact and preventing the app from slowing down. I'm curious if there's a similar technique available in Swift or if you might have other suggestions to address this issue.
- Someone 3y ago> the app begins to significantly slow down due to the need to redraw every stroke continually. Why the continually? If the screen shows the result of drawing all strokes, and the user draws a new one, you only need to draw that new one. If redrawing gets slow, I would save both the strokes and their current rendering and, on redraw, just blit the rendering. Also, in case you haven’t seen them: https://developer.apple.com/documentation/pencilkit https://developer.apple.com/documentation/pencilkit or the example code at https://developer.apple.com/documentation/pencilkit/drawing_with_pencilkit https://developer.apple.com/documentation/pencilkit/drawing_....
- bubobubobo 3y agoThanks for the great example of drawing stroke using UIKit! It was really helpful for refactoring my code. Also I figured out that another problem was the stroke saving logic(local & server) runs everytime I stroke. Thanks again for the comment.
- gus_massa 3y ago(I'm not sure if it's related, but I hope it can give some ideas.) In Classic VB6, I had a similar problem. When you had a magical "autoredraw" picture, everytime you draw all the surface is repainted. The solution was to use a hidden image and draw there, and also draw in another image the screen, and when another windows uncover part of the screen use the hidden image to fill the location.
- bubobubobo 3y agoI tried using an image view instead of UIGraphicsContext, but the performance was not very good. Thanks for suggestion, it was nice idea!