I am designing this app and there's a section where the users are allowed to draw with their fingers. As I provide a un开发者_StackOverflow中文版do for the operation, when the user starts drawing I have to quickly grab the current drawing content and store it somewhere.
I first tried to store the undo as a CGLayer and also as an image, but my app memory usage went up from 7 to 19 Mb. With 19 MB I am relatively safe, because 24 Mb appears to be the theoretical limit beyond things start to be dangerous. The problem is that I have another section of my app that requires lots of memory and if I run this, the memory peaks from 19 to 28 Mb, that is too dangerous to risk.
Then I decided to save the image on disk. To prevent the little gap that happens when the image has to be saved when the user fires TouchesBegan I refined, to the limits of sanity, the saving to disk method and now I barely don't feel any gap. I said, I barely don't feel, but I still feel a little hair gap, I would say <0.1s that takes to the line start drawing.
What I do is to fire a queue operation to manage the file saving.
Is there any other ways you guys can envision on how could this be improved?
thanks
Use mmap
and CGBitmapContextCreate
to create images that are backed by a file; the kernel will lazily page in and out parts of the file as they are needed.
Combine this with rjobidon's suggestion by snapshotting every so often and you should have a robust and speedy undo system.
I suggest to store the image as vector data, this way you could play and undo very quickly and divide by thousand times the memory used. For example you can only store:
- Gesture coordinates (x, y)
- Gesture type (touchBegan, touchEnded)
- Pen changes (color, width, effect)
You also have a canvas to render the current image.
Good luck!
How about doing the storing at the end of the previous operation? Then the beginning of each stroke will be instant.
You could even do it on a timer so it only saves an undo if you pause drawing for a few seconds, which would mean the undo takes them back to the previous stage of the drawing, rather than just the previous stroke, which may be desirable.
精彩评论