Some Objective-C Helpers for Data Wrangling
Getting my feet wet in a new language means finding groups of functions which allow you to do traversal. Usually, the woes that go with this process revolve around shifting data from what format to another. In the case of objective-c, all objects are relatively analogous to each other. Additionally, as a message-based language, you can essentially run functions on your objects and if they can called, they will, otherwise it will simply ignore you. Here’s a bit of a breakdown of code snippets I’ve been snagging, for use in various aspects of iPhone development specifically. Additionally, a roundup of some online resources I’ve been referring to at the bottom of the post.
Throwing Image Data into Arrays, Sets, Dictionaries
There are multiple ways of drawing image data out, but this way draws directly into a graphics context, which is good for doing procedural animation (ala processing/computational visualization)
//throwing it in from jpg in resources folder
UIImage *myImage = [UIImage imageWithContentsOfFile:@"myImage.jpg"];
NSArray *myArray = [NSMutableArray array]; // this will autorelease
[myArray addObject:myImage];
//drawing back out
CGContextRef context = UIGraphicsGetCurrentContext(); // get GC
UIImage *myImage = [myArray lastObject];
CGImageRef *myCGImage = [myImage CGImage];
CGContextDrawImage(context, rect, myCGImage);
Converting JPEG/PNG Images in NSImage into NSData
NSData *imageData = UIImagePNGRepresentation(image); // png
NSData *imageData = UIImagePNGRepresentation(image); // jpg/jpeg
Grabbing a quick URL's data for (for quick API calls)
NSString *text = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];
if(text){
textView.text = text;
}
Additional Resources
Making a POST of image/metadata to tumblr (easy to migrate to another receiving server)
Using JSON in Obj-C/iPhone
Learn Obj-C Shortlist
Nick Myer's Tutorials
Simple linking to MP3 streaming content
UITableView Overview
Please Leave a Reply
TrackBack URL :