This is my first question here. I have a notepad app that you can draw on, change pen size, change colors, etc.
The drawing is done in core-graphics code in the view’s drawRect: method after detecting the touches. Now I want to be able to save this drawing to Core Data or a text file doesn’t matter and be able to load it again and keep drawing. Can I save it as NSDATA with imagePngRepresentation? It is bitmap then and I can’t keep drawing again.
Please help me what is the best way to do this, to save the drawing and then be able to load it up again to be able to continue drawing?
Thanks a lot!!
I know this is late, but I spent the past few hours figuring this out myself. So here goes:
In nib/xib files (and now, storyboards), you can ‘freeze dry’ your views in a file by constructing your interface in Interface Builder. Essentially, what it is doing here is archiving these objects and storing them in a file. Wouldn’t it be great if users could do this to views in your app? Well, they can!
Apple’s guide on archiving really helped. I read it from front to back.
For each UIView subclass you have, implement
encodeWithCoder:Just a note about this: some objects do not conform to the NSCoder protocol. For instance, I use UIGestureRecognizer objects in some of my UIView subclasses. Since they don’t conform to the protocol, I just recreate them when the view is unarchived with
initWithCoder:. So make sure you have a way of retrieving objects that you can’t archive!In
initWithCoder:To save the view:
Finally, to load the view and its subviews:
Hope this helps!