Journal of a Maker

A technical and personal blog by Warren C. Moore, iOS Developer

UIView Rounded Corners

This is just a quick note regarding UIViews and rounded corners.

As you may know, every UIView is backed by a stack of CALayers, a by-product of the fact that compositing in iOS is managed by Core Animation. This allows you to configure a custom masking layer on top of the primary backing layer of a view.

In the past, I’ve used Björn Sållarp’s UIImage categories to redraw images with corners as needed, with a little bit of custom magic to select which corners are rounded.

Although it is probably more performant to render an image once than composite it every time the view draws, being able to instantly select which corners are rounded is sometimes an affordable luxury. You might disagree, and Michael Ayers has done some recent work on squeezing performance out of situations like this.

But, with the goal of keeping things simple and dynamic in mind, I created a very small category on UIView that allows you to select a corner radius and a set of corners to round. The core algorithm is shown below, followed by a link to the public github repo.

1
2
3
4
5
6
7
8
9
10
11
12
13
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, minx, midy);
CGPathAddArcToPoint(path, nil, minx, miny, midx, miny, (corners & UIViewRoundedCornerUpperLeft) ? radius : 0);
CGPathAddArcToPoint(path, nil, maxx, miny, maxx, midy, (corners & UIViewRoundedCornerUpperRight) ? radius : 0);
CGPathAddArcToPoint(path, nil, maxx, maxy, midx, maxy, (corners & UIViewRoundedCornerLowerRight) ? radius : 0);
CGPathAddArcToPoint(path, nil, minx, maxy, minx, midy, (corners & UIViewRoundedCornerLowerLeft) ? radius : 0);
CGPathCloseSubpath(path);

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
[maskLayer setPath:path];
[[self layer] setMask:nil];
[[self layer] setMask:maskLayer];
[maskLayer release];

View code on Github. Feel free to fork away and send suggestions. MIT License.

Automatically Generate iPhone/iPad Icons at All Required Sizes

With the advent of the iPhone 4 Retina Display, iOS application developers have an even larger task when it comes to creating the various sizes of icons they need for their applications. In summary, here are the icon sizes and where they’re applied:

  1. 57x57 - iPod touch, iPhone, iPhone 3G, iPhone 3GS main screen
  2. 29x29 - iPod touch, iPhone, iPhone 3G, iPhone 3GS app settings and Spotlight, iPad settings
  3. 72x72 - iPad main screen
  4. 50x50 - iPad Spotlight
  5. 72x72 - iPad main screen
  6. 114x114 - iPhone 4 main screen
  7. 58x58 - iPhone 4 settings and Spotlight
  8. 320x320 - iOS documents
  9. 64x64 - iOS documents

You may wish to customize your document icons (e.g., by overlaying your icon on a page icon or somesuch). And of course, iTunes requires that you submit a 512x512 image to be used in the App Store and elsewhere. This is often customized but will likely be similar in style to your primary app icon.

If you’re like me, you don’t have a graphic designer operating at the pixel level to produce sharp, snazzy icons at all those sizes. So instead of tooling around in Photoshop every time you tweak your 512x512 master icon (Image -> Image Size… -> 57px -> Save As… -> Undo -> ad infinitum), why not automate the process?

I’ve put together a small (tiny) XCode project that uses AppKit to output all the icon sizes mentioned above. And it’ll be easy to tweak when the iPad with Retina Display (fingers crossed) debuts and needs 96px and 144 px versions of your icon. Throw it into your build system and get freshly-resized icons every time you compile. Grab it here:

Click to download

iPhone SDK: Making Your AudioSession Routes Play Nicely With the Vibrate Switch

So, you’re writing an awesome multimedia application for the iPhone. Everything’s going swimmingly, and you even have some nice animation effects for when the iPhone is plugged into an external accessory, just like the iPod app:

1
2
3
4
5
6
7
8
9
- (void)toggleVolumeDisplay:(BOOL)show {
  BOOL isShowing = (volumeSlider.alpha > 0.0);
  if( isShowing && !show ) {
    // Animate volume slider down and out, while animating play controls to center of view 
  }
  else if( !isShowing && show ) {
    // Animate volume slider in, while animating play controls up to the top of the view 
  }
}

Or something like that. That’s purely to illustrate what you might be doing in response to a run-of-the-mill hardware route change (headphones plugged in, connected to speaker dock, etc.), which of course you’re detecting like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// When the view is loaded
AudioSessionAddPropertyListener( kAudioSessionProperty_AudioRouteChange, RouteChangeListener, (void *)self);

// Called later as part of your listener callback 
CFStringRef state = nil;
UInt32 propertySize = sizeof(CFStringRef);
OSStatus result = AudioSessionGetProperty( kAudioSessionProperty_AudioRoute, &propertySize, &state );
if( result == kAudioSessionNoError) {
  if( CFStringGetLength(state) > 0) {
    if ([(NSString *)state isEqualToString:@"LineOut"])
      // only special case we care about 
      shouldShowVolumeControl = NO;
    } else
       ; // vibrate switch engaged
  } else {
    if (result == kAudioSessionUnsupportedPropertyError)
      shouldShowVolumeControl = NO; // probably simulator 
    else
      ; // error encountered 
}

This has been documented in plenty of places. Then, you flip the Ring/Vibrate switch and everything grinds to a halt. Your route change callback doesn’t get called, and now your MPVolumeView declares to the world that the volume isn’t available. Of course it’s not available. That’s why we’re going to all this trouble - to hide the volume slider when we aren’t permitted to change it!

After a long time pondering this problem (it’s been on the backburner for about a week), I eventually realized that the route change callback might be dependent on the so-called ”category” of the AudioSession. For example, if your app just plays alert sounds occasionally, you (probably) don’t want them sounding when the phone is set to vibrate. On the other hand, it’s critical for audio players like the iPod app to be able to play even when in vibrate mode. So, how do we go about changing our session category and getting those mysteriously absent route change callbacks? Like so:

1
2
UInt32 audioCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(UInt32), &audioCategory);

This tells the AudioSession that we want to be regarded as a ”media playback” app, rather than, for example, an “ambient sound” app. Now, regardless of the position of the vibrate switch, we get notifications when our iPhone is docked and undocked from external accessories, and we can animate our volume controls as we desired.

Push

For the past year, I’ve been employed by 352 Media Group, based first in Gainesville, FL and then in Atlanta, GA.

Recently, I decided that the time had come for me to try to make it as a freelancer. I’m not the kind of person who thinks everyone should follow the path of self-employment. On the other hand, there comes a point in some of our lives where the idea of working for someone else is so distasteful that the inevitable pain and isolation that comes from going it alone is actually attractive.

In other words, it isn’t all roses. I’m extremely optimistic about what the future holds. I know that my skills are valuable once they get in the right hands. But for the past year, I’ve had people above and around me figuring out how to distribute my skills, and now that’s all on me. Project management, IT, business development, marketing, public relations, HR… they all just collapsed into the singular incomprehensible mass that now rests before me.

Back in August of 2007, I had this to say about my expected trajectory. I’m reposting it here, unedited, because I think it’s important to remember that I was as clueless then as I feel now, and things have gone pretty well so far.

One of the most striking patterns that has shaped my life is the exponential feedback of intentionality. What I mean is, I seem to be eerily good at making things happen that I set my mind to. Before I ever spoke to Microsoft at the Fall 2006 job fair at UF, I wrote a Post-It note and stuck it to my mirror: “I will work for Microsoft.”

Several months later, I was. Before that, I had opportunities in tech support, web application development, and game development that were greatly aided by just thinking hard about what I wanted.

The key to manifesting intentions is to let your subconscious mind discover opportunities for you, because you could easily burn 25 hours a day looking actively for them. As long as you keep your goals (which should take no more than about 20 words to state) in your mind at all times, following your “heart” (really your mind giving you hints about the correct choice of action) will cause you to converge on your goals.

What happens when you don’t keep specific goals in mind? You will consistently converge on whatever occupies your mind for the moment, or on nothing at all. One way or another, you’ll notice that you spend a lot of energy just getting by.

I can’t conceive how many of my peers accept the reality that shortly (in about 10 months for some of us), they will be sitting at desks in whatever big company pities them with an entry-level position and 3 days of vacation doing menial coding tasks that amount to a piss-take in the ocean of personality-free software.

This is not the corporate America of your father. $30 a hour is not enough for me. $200 an hour isn’t enough either. Not if I give up control. Not if I’m beholden to someone whose seniority will always dictate their position (above me) in the org chart.

These are the reasons I will not accept a safety net once I graduate. It’s all or nothing, because failure is never final. Failure is just one more motivation to try harder. The alternative to trying again after failure is death.

So, come what may. King me or kill me.

Truer words. But words without commitment or substance. So, the fresh realization is that there is no substitute for cutting the cord for real. For shipping. For taking clients and failing or succeeding on my own merits and not within someone else’s support structure. It took awhile to get here, but here goes…

Make Terminal Follow Aliases Like Symlinks

Okay, so you’re spelunking around in Mac OS using Terminal. You try to ‘cd’ into a directory only to be told that what you’re trying to get to “is not a directory.” Then you remember that the target directory is actually a shortcut that you created with Finder. It looks just link a symlink in Finder, so shouldn’t it act like one in Terminal?

_ _

Unfortunately, in OS X, aliases are treated differently by the command line than symlinks. In particular, they won’t be followed by the “cd” command, leading to your present frustration. Fortunately, with a little elbow grease, you can patch up your shell and be on your merry way.

This is a two-part process requiring a little familiarity with gcc and bash, but I’ll try to make it as simple as possible. Firstly, you need this file: getTrueName.c. This file was created by Thos Davis and is licensed under the GPLv2. Save it anywhere, then compile it with the following command:

1
gcc -o getTrueName -framework Carbon getTrueName.c

This will create the ‘getTrueName’ executable in the same directory as the source. You can add it to your PATH, or just copy it directly to /usr/bin so it’s easy to access.

Interestingly, when Terminal opens a new shell, .bashrc is not executed as you might expect. Instead, under the login shell, .bash_profile is executed. So, add the following to .bash_profile in your Home directory. You might need to create it first; it isn’t there by default.

1
2
3
4
5
6
7
8
9
10
11
12
13
function cd
{
  if [ ${#1} == 0 ]; then 
    builtin cd 
  elif [ -d "${1}" ]; then 
    builtin cd "${1}"
  elif [[ -f "${1}" || -L "${1}" ]]; then 
    path=$(getTrueName "$1")
    builtin cd "$path"
  else 
    builtin cd "${1}"
  fi
}

Effectively, this looks for Finder aliases and resolves them before deferring to the builtin cd command. Append it to your .bash_profile, then either execute it or restart Terminal for the changes to take effect. Now you can cd to Finder aliases within Terminal and have them treated just like symlinks. Just like it should be.

Creating Value at Starbucks

A recent article by Tom Davenport on the Harvard Business Publishing metablog has me thinking. Entitled “Is Web 2.0 Living on Thin Air?,” the post asks whether all us hipster Starbucks jet-setting metrosapiens are really creating value, or just participating in a “fluffy” game of social media- powered self-delusion. I think it’s the wrong question to ask, but because Davenport implicitly answers another question, this present post is vindicated and the fundamental issue at hand is revealed as both beginning and end.

Concordantly, allow me to ask the question that Davenport really wants to ask: Regardless of the degree of engagement in often frivolous social networking activities (i.e., poke wars, media tagging, and adding edges to the social graph just for the sake of increasing your friend count), who is creating value and in what is that value based?

Speaking as an aspiring coffee shop hipster creative, I can tell you that when I’m sitting down with a Grande Pike Place drip brew, I’m not going to be spending much time on facebook; I’m going to be spending time slicing .PSDs, writing CSS, and scripting PHP and jQuery, or my language du jour.

I remember reading a much more conservative piece by an individual I could only imagine being the type of neo-maxi-zoom-dweebie who could make a career of doling out largely vapid and condescending advice. Substantial Googling and Snopesing turned up Charles J. Sykes and the relevant quotation: “Television is not real life. In real life people actually have to leave the coffee shop and go to jobs.” Surely enough, he’s at it again. Suffice to say, this kind of polemic fails to inspire me. I guess my necktie is a little too loose for me to get the point.

It turns out that it’s possible to create value darn-near anywhere these days. I’m not saying every job is implicitly mobile; vanishingly few are. But for those of us knowledge workers who produce the technology that makes everyone elses’ lives richer and easier, a favorite coffee shop can, as in the vision for Starbucks envisioned by Howard Shultz, provide a comfortable “other place,” a sacred venue away from the distractions of home where, on the best days, everything but the workpiece fades into the background and we’re cruising along in the zone of maximum productivity.

In short, the value we produce takes the form of applications and systems that make it easier for people to do whatever it is they do. So indeed, getting overly engaged in social networking can be a big drain on personal productivity. But we shouldn’t confuse such value-sapping minutiae with the value-creating work of producing sites that encourage engagement, including, yes, the creation of social media sites.

Blog Action Day 08: Moving Slowly on Poverty

In the last hour or so of the day (on the West Coast, at least), and even though this site is still under very active construction, I wanted to drop a quick post for Blog Action Day ‘08. The topic of the year is poverty.

As I am a white Anglo-Saxon male who grew up, if not in, at least adjacent to, the suburbs, most people would assume that my experience with poverty is minimal. They would be correct. I have never known clawing hunger for days on end; have never lacked a roof, four walls and a carpeted floor; have never been the subject of an exposé on wretched conditions in the inner city nor a child with baleful eyes who could get by with a mere 75 cents a day.

No, being in the first world, I like to imagine that I can get by, due to my place of privilege in Maslow’s hierarchy, on just my principles. One of my friends recently posted on facebook a quotation by Protestant minister William J. H. Boetcker, from which I take the following excerpts: ”You cannot strengthen the weak by weakening the strong[…]” and ”[__You] cannot help men permanently by doing for them what they can and should do for themselves.” As much as this sentiment reeks of the hackneyed arguments people make for trickle-down effects between classes, there is something to be taken from it, and it has everything to do with urgency.

Obviously, the time and money dumped into efforts to “elevate” the third world has benefits, up to and including saving lives that would otherwise be lost. But is this naive approach adequate when the very lives that are saved lack the infrastructure, knowledge, and other resources necessary to perpetuate it? This is what Boetcker meant when he said, “you cannot help the poor by destroying the rich.” If all the wealth generated by the top 3% of earners in the world went to feed, clothe and shelter children in states of poverty, we would have not one but two destitute generations as a result.

The good news is that “elevation” doesn’t have to entirely recapitulate the history of the Western world. We routinely design and commoditize hardware that is decades ahead of that available in the poorest areas abroad, which means that the adoption cost in those areas is a tiny fraction of the initial development cost - yet another instance of the way in which the pricipal of a personal investment can be multiplied many times over.

So, in moving on poverty, the answer is not strictly an influx of capital. Obviously large humanitarian organizations must be aware of this at some level, because their operations are not as naive as the base case described above. But the broader population should be grounded in firmer realities than the swollen bellies of starving children half-way around the globe. These images are tragic and unsettling, and run the risk of inducing despair or a kind of detached revulsion; instead, where is our collective plan for introducing infrastructure and slowly burning poverty up in the flames of progress and accrual of benefits from both social and monetary investments?

It’s a humanity-sized goal, but for as slow as it would seem to be, it’s the fastest way to see the payoff we’d all love to see.