I have a question concerning the placement of CoreGraphics drawing code. I have a C struct which is a representation of my model and I have a custom view class. This custom view class implements a drawRect and draws it using CoreGraphics.
I now need the ability to also generate a PDF from this view to export it. It uses the exact same drawing code as used for the view.
My solution right now is to have a separate drawing method in the custom view class and using this method in the drawRect method and my method to generate the PDF.
I am not sure if this is a clean solution because the view has a function which has nothing to do with drawing on screen. However using a new custom class as my PDF generator would mean duplicating my drawing code.
Is there a clean way to organize this common drawing code? One could use a helper object and have it as an instance variable but this would make it a bit bloated.
Cocoa’s printing architecture automatically uses the view drawing code when printing, and printing can be used to create a PDF file. This means that you can simply place your custom view drawing code in
-drawRectand then do something like: