Intermediate Core Graphics | Ray Wenderlich

Continue learning Core Graphics drawing in this intermediate series. You’ll build on the skills you learned in the Beginner series and learn about gradients, transformations and creating PDF files.

This is a companion discussion topic for the original entry at
1 Like

Dear Caroline, thank you very much for the great tutorial! I wonder whether you have a hint for me regards the following type of CGContext: <CGContext 0x28387cd80> (kCGContextTypeIOSurface). For example, PDFKit’s PDFPage’s method “func draw(with box: PDFDisplayBox, to context: CGContext)” uses it. If you are running an App on a device, override this function and add a “print(context)” into the function body, then the above is what shows up in the console. (On simulator, it will print out that it is a bitmap context.) I wonder how I can get THIS type of context myself so I can draw into it. Neither the Core Graphics documentation nor the IOSurface documentation seem of help, and a web search did not help me either. Creating an IOSurface and passing its address as the “data: UnsafeMutablePointer” input into the Bitmap Graphics Context initialiser is not successful (not too surprising of course…, the initialiser does what it should: It constructs a context of type kCGContextTypeBitmap). And by using UIGraphicsImageRenderer I get a context of type kCGContextTypeAutomatic. The reason I want a context of type kCGContextTypeIOSurface is to see whether it is faster for certain type of offscreen drawings (for example ones where there is a lot of blending going on), as compared to the bitmap and automatic contexts… I can unfortunately not use Metal in this case, as my draw function itself is a black box and all it knows is to draw into a CGContext. Do you know anything about this IOSurface type CGContext and how to initialise one? Is the context constructor maybe not public API? (IOSurface API seemingly became public API not so long ago…) Thank you very much for your thoughts on this!

Hi @celine.jost - No, I’m sorry, I can’t help you with that one. I haven’t used IOSurface at all.

Thank you anyway! Thank you also for replying so swiftly!

1 Like

Very late to the party, but in case you or anyone else is still interested: If you

override func draw(with box: PDFDisplayBox, to context: CGContext)

in your subclass of PDF page, then the context you are getting here is of type kCGContextTypeIOSurface.

Note that this is called a couple of times for each page because the PDF page is basically rendered out in tiles. It is called in background threads, but still awfully slow. Unfortunately I haven’t figured out a way to get the tile size in a non-hacky way, so it’s hard to clip expensive parts from drawing because they are not in the frame.

Thank you :slight_smile:. There’s not a lot of documentation on IOSurface unfortunately.

1 Like