Take the 2-minute tour ×

What is the best way to use the custom UI graphics on the iPhone?

I have come across CGContextDrawPDFPage and Panic's Shrinkit. Should I be using PDF's to store my vector ui graphics and loading them using CGContextDrawPDFPage to draw them.

Previously I asked what way Apple store their UI graphics and was answered crushed png. The options are I can think of are below, but I'm also interested in any other techniques...

  1. PNG (bitmapped image)
  2. Custom UIView drawing code (generated from Opacity)
  3. PDF (I've not used this method, is it with CGContextDrawPDFPage?)

This question is for vector graphics only (but I guess some people may only use bitmapped?). Looking for what is standard / most effective / most efficient.

Edit: Bounty added, I'm interested to hear the process of anyone who works with UI designers, or are themselves a UI designer. And pointers on resolution independence i.e. for iPad / iPhone HD future proofing.

Many thanks Ross

share|improve this question
Be wary of CGContextDrawPDFPage, it leaks memory unless you release the PDF document itself. See: lists.apple.com/archives/quartz-dev/2010/Jan/msg00027.html –  Josh Knauer Aug 13 '10 at 13:13
add comment

2 Answers

up vote 1 down vote accepted

I can suggest 3 different ways, 2 of which you already mentioned:

  1. Creating custom UIView.
  2. Drawing in a CGLayer.
  3. loading from PDF.

Each have their advantages, depending on what you want to do:

UIView vs CGLayer

In terms of performance (for one-time drawing) and ease of use there shouldn't be much difference between the two (there are minor differences, but nothing serious). Apparently Opacity can export source code for both (I haven't personally used it). That said, there are things you should consider before choosing:

  1. If you have a fixed image (which your question suggests so), use CGLayer. CGLayer objects will be cached on the graphics device, so re-using them is much faster. Even if the cache is cleared, you're still using the same object for redrawing, meaning there's no need for re-creating it.
  2. On the other hand, if you need to change your drawing as the user interacts with the app, UIView could be faster, as you have the flexibility of updating just one part of the image instead of the whole view.
  3. CGLayer is independent of the UI. So the same code works fine for Mac/iPhone/iPad, or even for saving to files.

Conclusion: Use CGLayer, unless it's a special case.

CGLayer (In code drawing) vs PDF (loading from file)

I don't have any benchmark for this, but I expect CGLayer to be slightly faster: (1) there's no need to read a file. (2) the pdf commands should be converted to system's graphic elements, which is more or less the same as creating a CGLayer. (3) I'm not sure about how pdf pages are cached, but I don't expect it to be faster than CGLayer. Anyway, all this shouldn't make much difference unless you want optimization till the last millisec. Again, the choice should be based on your use case:

  1. CGLayer gives you more flexibility in the code. Your only access to a pdf page is through CGContextDrawPDFPage, which means even simple tasks such as scaling/transforming the drawing will be harder.
  2. Using PDFs on the other hand, is more flexible after finishing the code. You can simply update the pdf file with a new whenever you want, load it from the web, etc. .
  3. Creating a pdf could be easier than coding the drawing. You can use any app you want, you don't need to worry about the API and system resources. After all, the code can output a pdf file, not the other way around.

Conclusion: If you don't need to do much with the drawing (just want to show an icon or something), go with the pdf. if you need to work on it in the app, consider CGLayer.

Of course you could always mix the approaches as you see fit: e.g. Load a pdf file, put it in a CGLayer to adjust it, draw it with a UIView where you can put a badge on it!

share|improve this answer
add comment

I stumbled over this question because I have a question about PDFs too. I'm working together with a UI designer and we are succesfully using PDFs to create UI elements. For example: For a button we have 3 PDFs for ON, OFF and the shadow. I wrote a piece of code that transformes a PDF into a UIImage. It can scale the resulting image and even colorize it to have one template for many styles of buttons. It works pretty good :)

Our problem is that we can't scale up the vector graphics without quality loss. That's why we decided to use graphics that are big enough and we only have to scale them down. But I still wonder if there's a way to scale up a PDF before drawing it to a context and create a UIImage. Here's my post.

share|improve this answer
Great. Thanks for putting a link to your question. –  Ross Jul 29 '10 at 10:49
add comment

Your Answer


