Archive for the ‘iOS’ Category

Building a Bezier path from a bitmap



I’m working hard on my UIKit / Quartz book, where I’m doing a lot of coverage of drawing using Bezier paths. Today, after a pretty detail-oriented day of writing, I was more than ready to take a break.

I headed over to IRC and encountered a particularly interesting challenge. It came in the form of Clarus the dog cow. She’s normally a bitmap, which isn’t something that translates particularly well into the world of UIImages. Surely, I thought, there should be a way to generalize her to a reusable path.

Inspired by my PaintCode write-up from today, I decided to create a generalized bitmap-to-Bezier path method to address this. What I ended up with is a category that transforms bytes into an easy-to-draw UIBezierPath.

@implementation UIBezierPath (BitmapExtension)
+ (UIBezierPath *) pathFromBitmap: (NSData *) data size: (CGSize) size
    Byte *bytes = (Byte *) data.bytes;
    CFIndex height = size.height;
    CFIndex width = size.width;

    if (height * width != data.length)
        NSLog(@"Error: size does not match data's available bytes");
        return nil;

    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    for (CFIndex y = 0; y < height; y++)
        for (CFIndex x = 0; x < width; x++)
            CFIndex index = y * width + x;
            if (bytes[index] != 0)
                UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(x, y, 1, 1)];
                [bezierPath appendPath:path];

    return bezierPath;

CGTransform3D and UIImage

Screen Shot 2013-03-14 at 5.35.21 PM

I spent several hours today (that I will never get back) attempting to force a view with a 3D transform to render properly into a UIImage as a favor for Aaron B. Long story short? Failed.

My most promising approach was this, where I attempted to read in an image pixel-by-pixel. Although it worked for the gross dimensions, the fine details were not properly read due to the z-axis rotation:

CGPoint p = [view.layer.superlayer convertPoint:refPoint toLayer:view.layer];

In the end, the lesson is this: 1. Don’t get distracted by Quartz stuff when you’re supposed to be writing a Quartz book. 2. Listen to Apple when it says, “Layers that use 3D transforms are not rendered”

On the bright side, I have marching ants working beautifully — integrated with CADisplayLink and a time interval you specify:  Marching Ants

Color Sampler sneak peek

iOS Simulator Screen shot Mar 12, 2013 12.37.02 PMI’m updating my color sampler app for a book I’m developing with Brett Terpstra on iOS Controls. I’ve built some fun interaction styles into the update. These include a scenario picker, the swatch selector deck you see here, a push-to-adjust color editor, and more.

Controls are great fun especially when you can generalize them. I enjoy moving them out from specific uses to a more reusable component. I especially love adding physical reactions using basic principles of animation.

You can preview a few of these controls in action over on my YouTube channel.

iOS Auto Layout Demystified: Arriving in April

Screen Shot 2013-02-27 at 10.00.54 AMMy first AW Mobile Programming title should be shipping in April. iOS Auto Layout  Demystified helps tame the constraints in your development project.

Auto Layout re-imagines the way developers create user interfaces. It provides a flexible and powerful system that describes how views and their content relate to each other and to the windows and superviews they occupy.

In contrast to older design approaches, this technology offers incredible control over layout with a wider range of customization than frames, springs, and struts can express. Somewhat maligned by exasperated developers, Auto Layout has gained a reputation for difficulty and frustration, particularly when used through Interface Builder.

That’s why this book exists. It helps you learn Auto Layout mastery by example, with plenty of explanations and tips. Instead of struggling with class documentation, you’ll learn in simple steps how the system works and why it’s far more powerful than you first imagined. You’ll read about common design scenarios and discover best practices that make Auto Layout a pleasure rather than a chore to use.

  • Chapter 1 Introducing Auto Layout
  • Chapter 2 Constraints
  • Chapter 3 Interface Builder Layout
  • Chapter 4 Visual Formats
  • Chapter 5 Debugging Constraints
  • Chapter 6 Building with Auto Layout
  • Chapter 7 Layout Solutions