I am designing this app and there’s a section where the users are allowed to draw with their fingers. As I provide a undo 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
mmapandCGBitmapContextCreateto 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.