// elrepositorio the random musings of Jeff Forbes

23Nov/080

iPhone Dev Tip #1: Taking a Screenshot

Since this is what I do for a living, I thought I'd start posting some tips for iPhone development. Most of the time, these will be cross-posted (or asked by me) on http://www.stackoverflow.com since I participate in the Q/A there. The site is a fantastic resource -- check it out.

Without further adieu, tip #1. This is a pretty important thing to do in a lot of cases and not easily understood by looking at the documentation in the SDK. Funnily enough, it can be boiled down to a few short lines:

UIGraphicsBeginImageContext(self.view.frame.size); //size of your screenshot

//render the view you want in the new image context
[[self.view layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
[image retain]; //this is an unretained UIImage, so let's save it
UIGraphicsEndImageContext();

That's it. There's a lot more to be said about CGContexts and the like, but a lot of the difficulty of working with these are simplified with the UIGraphics*() functions. Search for them in the XCode documentation.

Tune in next time for another helpful tip! :D

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.