Core Data

It came ever so suddenly the point in my application that i needed to start saving things in my app, not just in memory but actually to a local database.

On iOS the obvious choice for this is Core Data, an object persistence framework that allows you to save objects to a sqllite db, and much, much more.

For the first couple of hours i was poking my way through it, somewhat baffled as to how it all worked. Coming from the Entity framework things don’t seem as clear cut, further inspection reveals however that it’s just different terminology for the same things (nearabouts).

The main cool thing about core data - undo/redo comes supplied. The drawback, lightweight migrations become confusing when you can’t see the actual backend data model matching your xcdatamodel; bit weird but you’ll get used to it.

I guess soon the thing will brake on me, or i’ll have some more complex need from it and i’ll get yet again very, very confused.

Another random thing, my app looks like crap; I’ve ran into Sketch which may solve my problems. Anyone have any more recomendations for creating beautiful iOS UI designs for non-designer types?

Core Text

As part of my new project (released in March, watch this space) i had the need to draw text in a core graphics context.

This needed a little research and if you look here the lovely folk at apple tell you to use Core Text.

My advice to you is unless you have some really advanced low level functionality, don’t even bother.

Because core text has foundations from OSX (and is a pain in the ass) it means you need to invert the coordinate system to draw anything, this is not good if you plan on drawing anything the normal way in core graphics, as you continuously need to switch.

Here’s what Apple fail to say anywhere in there long list of documentation, just use the NSString method:

drawInRect:withFont:lineBreakMode:alignment:

It’s much easier and will most likely do everything you want.

Hope that helps someone who’s looking for an easy way to draw text using core graphics.

Here we go with the new project! I’ve done enough bloody reading now to just get on with it! can’t wait to see the results once it starts coming together properly!

Building things is fun!

Here we go with the new project! I’ve done enough bloody reading now to just get on with it! can’t wait to see the results once it starts coming together properly!

Building things is fun!

Web Programming

Being somewhat of a “never say never” type of person when it comes to learning, I had no experience of web programming (really) before I accepted creating a pretty difficult site for my work.

I was also naive enough to tell them 2 months completion time which has come back to bite me in the ass, as it’s now a reality.

But, through it all I’ve gotten the hang of ASP.NET MVC remarkably quickly and actually implemented a small feature that I thought would be pretty darn hard (turns out it wasn’t).

Strange as it sounds i’m somewhat starting to enjoy web programming (scary) and the flow this framework provides is something great!

The results aren’t as great as graphics programming, but the code is not anywhere near as complex, and its a great skill to have! game on

After travelling to Switzerland last week to do visuals at Burning Mountain festival, i can happily say that this was one of the highlights.

True Swissstyle!!!!

Recently the company i work for ran into some interesting data, they asked me to do something with it and so naturally i accepted!This is the end result and i think its looking quite nice, it has support for dynamic manipulation to find data about the points on the map. Credit to Francis Williams for the stacked area graph and bar graph.

Recently the company i work for ran into some interesting data, they asked me to do something with it and so naturally i accepted!

This is the end result and i think its looking quite nice, it has support for dynamic manipulation to find data about the points on the map. Credit to Francis Williams for the stacked area graph and bar graph.

Objective C & OpenGL image exporting

I’ve just spent many hours trying to get something that is essentially extremely simple working with Opengl and Objective C

Exporting an image in something like Processing is easy, you just say beginRaw() and endRaw() and just put your drawing content in between, being used to this I’m amazed the folks at Apple haven’t come up with something similar for OpenGL subviews.

Anyway the problem with OpenGL is that its a raster based system, which basically means exporting vector based images is a no go unless you fancy a headache, so I’ll settle for lousy bitmaps.

Just to help people I’ve shared the code to return NSData from a pixel buffer of your OpenGL view, this works quite well and renders out a PNG, although you can specify more formats if you so please, heres the basics on using this code.

First create an action, say that is generated from a button, mine looks like this in code:

-(IBAction)savePDF:(id)sender

{

    __block NSSavePanel *panel = [NSSavePanel savePanel];

    [panel setAllowedFileTypes:[NSArray arrayWithObject:@”png”]];

    [panel beginSheetModalForWindow:[self window] completionHandler:^(NSInteger result) {

        if(result == NSOKButton){

            //NSRect r = [self bounds];

            NSData *data = [self glToUIImage];

            NSError *error;

            BOOL successful = [data writeToURL:[panel URL] options:0 error:&error];

            if(!successful){

                NSAlert *a = [NSAlert alertWithError:error];

                [a runModal];

            }

        }

        panel = nil;

    }];

}

Now notice theres a call to [self glToUIImage]; in there, implement this method like so.

 

-(NSData *) glToUIImage {

    NSRect baseRect = [self convertRectToBase:[self bounds]];

    NSInteger myDataLength = baseRect.size.width * baseRect.size.height * 4;

    int width = baseRect.size.width;

    int height = baseRect.size.height;

    // allocate array and read pixels into it.

    GLubyte *buffer = (GLubyte *) malloc(myDataLength);

    glReadPixels(0, 0, baseRect.size.width, baseRect.size.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

    // gl renders “upside down” so swap top to bottom into new array.

    // there’s gotta be a better way, but this works.

    GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);

    for(int y = 0; y <height; y++)

    {

        for(int x = 0; x <width * 4; x++)

        {

            buffer2[((height - 1) - y) * width * 4 + x] = buffer[y * 4 * width + x];

        }

    }

    // make data provider with data.

    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);

    // prep the ingredients

    int bitsPerComponent = 8;

    int bitsPerPixel = 32;

    int bytesPerRow = 4 * width;

    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();

    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;

    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

    // make the cgimage

    CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

    // then make the uiimage from that

    CIImage *myImage = [CIImage imageWithCGImage:imageRef];

    NSBitmapImageRep* rep = [[NSBitmapImageRep alloc]

                             initWithCIImage:myImage];

    NSData* PNGData = [rep representationUsingType:NSPNGFileType

                                        properties:nil];

    return PNGData;

}

Thats it! you should be okies to export from an OpenGL view, make sure you connect the action to a component and your good to go!

A kinect music visualizer i made using Processing and a bunch of libraries. 

DMX for Mac

Over the past month i’ve been slowly moving away from Java and onto Objective C, a move I’ve been waiting to take since finishing my MSc thesis.

Now getting to grips with Objective C, I’ve done some research on the app store to see what DMX software packages they have.

Currently some basic software exists for the purpose of control, however there aren’t many packages that support mapping integration from multiple input sources.

The Plan:

Supposedly things are more likely to get done if you write them down, the plan for one half of this year is to write a lighting control system in objective c for the mac app store. 

This will at first include five main sources of mapping integration: Music, WiiMote, OSC, Wave Functions, and the Kinect.

The interface will be grid styled, and easy to use, with more emphasis on simply playing with lights as opposed to the troublesome task of setting them up onto a network.

This work is inspired by Light jams, and i hope that the developer allows me to utilise his branding for a mac version.

Sometimes i wonder how much time I have on my hands, write up due in on the 30th november and i just spend an hour creating a logo - oh dear!

Sometimes i wonder how much time I have on my hands, write up due in on the 30th november and i just spend an hour creating a logo - oh dear!