[ANN] MPWDrawingContext, pleasant Objective-C drawing context

  • MPWDrawingContext is a light-weight Objective-C wrapper around CoreGraphics CGContextRef and corresponding functions.

    Code is on Github:    https://github.com/mpw/MPWDrawingContext

    Infrequently Asked Questions:

    Why would anyone need an Objective-C drawing context?

    In short, while CoreGraphics is an awesome graphics subsystem, not having OO features makes CGContext closed to extension by anyone but Apple, and somewhat unpleasant to use, IMHO.

    I explain a bit more about the motivation on my blog:  http://blog.metaobject.com/2012/06/pleasant-objective-c-drawing-context.htm
    l


    Who cares about possible future expansion when that means there's lots of code to integrate with nasty dependencies?

    1 Class,  1 Protocol, 3 extra include files to equalize some of the differences between iOS and OSX (could probably be reduced).

    1 additional class (MPWView) is purely optional

    In the github project, the code is actually integrated into an adapted version of Matt Gallagher's IconApp, so you have a working example right there.

    But Cocoa has some fine drawing functionality with NSBezierPath, NSAffineTransform and friends

    True, but MPWDrawingContext works identically on both iOS and Mac OS X.  In fact there's also an MPWView class that works on both iOS and OSX, which is used in the sample code mentioned above to create an iOS app using the same drawing code as the OS X app.

    I also prefer my graphics context to not be a hidden global parameter that's implicitly used by a bunch of other objects.

    Who cares about OSX ^ iOS?

    Based on my unscientific experiments, MPWDrawingContext reduces the code I have to write for even one of the two platforms by about 20-30%.  Your mileage will almost certainly vary.

    Who cares about less code?

    Well, it\s not just less code, it's more pleasant code as well:

    [[[[[context moveto:0 :0] lineto:100 :0] lineto:50 :50] closepath] stroke];

    vs.

    CGContextMoveToPoint( context, 0, 0 );
    CGContextAddLineToPoint( context, 100, 0);
    CGContextAddLineToPoint( context, 50, 50 );
    CGContextClosePath( context );
    CGContextFillPath( context );

    And

    bitmapContext = [MPWCGDrawingContext rgbBitmapContext:NSMakeSize( 595, 842 )];

    vs.

    bitmapContext = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0,
                              CGColorSpaceCreateDeviceRGB(),
                              kCGImageAlphaPremultipliedLast)  | kCGBitmapByteOrderDefault );

    No it's not!

    OK :-)

    Marcel
  • On Jun 16, 2012, at 12:37 PM, Marcel Weiher wrote:

    > MPWDrawingContext is a light-weight Objective-C wrapper around CoreGraphics CGContextRef and corresponding functions.

    This sounds really cool and useful!

    What version of MPW do I need to build/run it? (<—joke for old-timers)

    —Jens
  • Hi folks,

    just a heads-up that I just pushed v 0.3 of MPWDrawingContext to github:    https://github.com/mpw/MPWDrawingContext

    The major enhancements have to do with using blocks for bracketing operations ( save/restore graphics state, shadow on/off, transparency layer begin/end) and delayed/repeated drawing, including patterns.  Especially the latter manages to unify a lot of slightly different code-paths and removes a lot of boilerplate.  You can let the context automatically choose the 'best' representation for repeated drawing or control the choice yourself with minimal changes to code.

    In addition, there's support for using single object arguments to messages like moveto:, lineto: and translate:, in addition to the versions taking two floats (moveto:: , lineto::, translate:: ).

    More here:  http://blog.metaobject.com/2013/01/more-objective-c-drawing-context.html

    Hope this is useful.

    Marcel

    On Jun 16, 2012, at 21:37 , Marcel Weiher <marcel.weiher...> wrote:

    > MPWDrawingContext is a light-weight Objective-C wrapper around CoreGraphics CGContextRef and corresponding functions.
    >
    > Code is on Github:    https://github.com/mpw/MPWDrawingContext
    >
    > Infrequently Asked Questions:
    >
    > Why would anyone need an Objective-C drawing context?
    >
    > In short, while CoreGraphics is an awesome graphics subsystem, not having OO features makes CGContext closed to extension by anyone but Apple, and somewhat unpleasant to use, IMHO.
    >
    > I explain a bit more about the motivation on my blog:  http://blog.metaobject.com/2012/06/pleasant-objective-c-drawing-context.htm
    l

    [...]
    > Who cares about less code?
    >
    > Well, it\s not just less code, it's more pleasant code as well:
    >
    > [[[[[context moveto:0 :0] lineto:100 :0] lineto:50 :50] closepath] stroke];
    >
    > vs.
    >
    > CGContextMoveToPoint( context, 0, 0 );
    > CGContextAddLineToPoint( context, 100, 0);
    > CGContextAddLineToPoint( context, 50, 50 );
    > CGContextClosePath( context );
    > CGContextFillPath( context );
    >
    > And
    >
    > bitmapContext = [MPWCGDrawingContext rgbBitmapContext:NSMakeSize( 595, 842 )];
    >
    > vs.
    >
    > bitmapContext = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0,
    > CGColorSpaceCreateDeviceRGB(),
    > kCGImageAlphaPremultipliedLast)  | kCGBitmapByteOrderDefault );
    >
    >
    > No it's not!
    >
    > OK :-)
    >
    >
    > Marcel
    >
previous month june 2012 next month
MTWTFSS
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30  
Go to today