How to Draw Shapes with Core Graphics

Matthew Campbell, August 19th

Would you like to know how to draw simple shapes directly on your iPhone app screen?

Most of the time we rely on UIKit to draw the images we use for our controls like buttons and other user controls. However, there are times when we would like to draw our own shapes or even let our users draw shapes on the screen. The technology that you use for this is called Core Graphics.

Core Graphics uses the idea of a context which is like an artist’s palate where we will do our drawing. When you draw with Core Graphics you must first define a color and width for a virtual pen and then give the pen instructions to move around the screen. Once you have done this you may call function that will execute your instructions and draw the line or shape.

The easiest way to experiment with using Core Graphics is to add a new UIView to your XCode project and then to add the view to a view controller. All the Core Graphics code must be located in the UIView implementation file (don’t confuse this with a view controller).

In my example project I first added a new UIView called CGView and then assigned an instance of this view to my view controller’s view property. Here is the relevant code from my view controller:

The easiest place to add the code the does the drawing in your UIView subclass is the drawRect method. Here is some example code I put in there that draws some lines and a rectangle. See the embedded comments for detailed explanations of what each line of code is doing:

When I build and run this project the output will look like this:

That is how to draw shapes – there are many other features to Core Graphics so be sure to check out the documentation on the Apple website.

Learn How To Make Your Own iPhone and iPad iOS apps!

If you are completely new to iPhone programming and want to start to developing iPhone apps this ebook will give you everything you need to get started today.

You Get the 5 Essential Things You Need to Make iPhone Apps

This ebook is much different than the other books out there: it really is a *system* that includes ONLY what you need to know to make iPhone applications. Not only that, but you will get some real insight on how to make a product and how to set up your code in the best way the first time.

Plus, you will get plenty of bonuses including exclusive source code projects. For more information, head on over to the website for the ebook:

Head over to How To Make An iPhone App eBook Website website to find out how to get Matt’s eBook and the bonus source code.

How to Draw Shapes with Core Graphics…

You’ve been kicked (a good thing) – Trackback from – iPhone SDK links, community driven…

[...] post:  How to Draw Shapes with Core Graphics Share and [...]

Just a little note to anyone that tries to do this using an UIImageView should note the following:

“The UIImageView class is optimized to draw its images to the display. UIImageView will not call drawRect: a subclass. If your subclass needs custom drawing code, it is recommended you use UIView as the base class.”

  • Hours of videos, demos & screen casts
  • Detailed HANDS-ON Exercises
  • Much Much Much More
  • UIKit
  • Objective-C
  • Core Data
  • More!
  • Learn How To Make An iPhone App right now using my proven system.

    PS: Tons of us are having a blast living off our own code, join us now. Click here to get all the details.