Ok, since this is a very useful piece of code for myself, I'm going to share it with you all.
In Cocoa on the desktop, you can draw whatever you like into an NSImage very easily:
[myImage lockFocus];
// draw
[myImage unlockFocus];
However, coming to Cocoa Touch on iPhone, without searching around a bit you won't find a way to do this. Well, thanks to Sam Steele (of Last.fm infamy), here's a code snippet which will do the exact same thing for you! Yes it's all in the docs, but takes a bit of looking to find.
UIGraphicsBeginImageContext(CGMakeSize(64.0f, 64.0f));
/* Put any sort of drawing code here */
[anImage drawAtPoint:CGPointZero];
[aView drawRect:CGRectMake(0.0f, 0.0f, 64.0f, 64.0f)];
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Hopefully this will help some budding iPhone developers out there.
CGMakeSize should be CGSizeMake, evidently
ReplyDeleteThanks for the sample code.. it's a great help!
ReplyDelete