4.0 Button Update

I’ve updated my Shiny Red Button code to render high-resolution buttons on high-resolution devices. This code must be linked against the iOS 4.0 SDK, but can target iOS 3.0. You can download the header and source files right away, or read a bit about the change below.


Here’s the only code that I had to change. I replaced this statement:


with this one:

if (UIGraphicsBeginImageContextWithOptions != NULL)
	UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);

The UIGraphicsBeginImageContextWithOptions function is obviously the key here; its third parameter specifies a “scale” factor to be applied (to the Current Transformation Matrix) when converting logical device coordinates (e.g., those supplied in size) into pixels (which are what Quartz understands). The 0.0 value supplied here is a “magic” number, which sets “the scale factor … to the scale factor of the device’s main screen”. This code, therefore, will render “normal” resolution buttons on 320×480 iPhones, and high-resolution buttons on high-resolution devices.

Weak Linking

The UIGraphicsBeginImageContextWithOptions function was introduced in iOS 4.0; this is why the new Shiny Red Button code must be linked against the iOS 4.0 SDK. Nevertheless, I wanted to let the code target iOS 3.0, since there are still a lot of those devices out there. Fortunately, AAPL makes this easy, by weak-linking many framework functions, including this one. This is why I begin the new code by testing for NULL; if the iOS 4.0 function isn’t available, I fall back to the original, 3.0-and-earlier function.

Share and Enjoy:
  • Twitter
  • Facebook
  • Digg
  • Reddit
  • HackerNews
  • del.icio.us
  • Google Bookmarks
  • Slashdot
This entry was posted in iPhone, Projects. Bookmark the permalink.

Comments are closed.