Making screenshots

April 28th, 2009 by Chris Leave a reply »

iPhone / iPod touch allows you to make screenshots (saves the contents as an image) by pressing both home and power button, although many users don’t know this functionality. If you go to your camera roll or sync photos with your computer, every screenshot you make is an image 320 x 480 px.

Today I will show you how to make a screenshot of arbitrary size using Quartz. What does arbitrary size mean? Well you can not only save the smaller part of your screen, but, if you for example have bigger view than 320×480 px you can save it all, not only the visible part.

Before you start, you have to add the QuartzCore framework to the project. All frameworks can be find here:

/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/ iPhoneOSxxx.sdk/System/Library/Frameworks

The part in bold can be different for each of you, in my case it’s iPhoneOS2.2.sdk. But there is much easier way to get to the Frameworks folder.

revealframeworkfinder

As you see above in Xcode, ctrl-press (or using your right mouse button) any of existing frameworks and choose Reveal in Finder.

finderframeworks

Now in Finder drag QuartzCore.framework to the Frameworks group into your project. It’s up to you if you decide to copy the item or simply link it. The framework has been added, now in the class which fill be making screenshots, you need to add a line to import the part of that framework:

#import <QuartzCore/QuartzCore.h>

You can add this line in either header (.h) or implementation file (.m).

Now it’s time to add the method which will make a screenshot and explain how it works:

-(void)saveToCameraRoll {
	UIGraphicsBeginImageContext(self.view.bounds.size);
	[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
	UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	UIImageWriteToSavedPhotosAlbum(viewImage, self, nil, nil); 

	/* 
	flashView.alpha = 1;
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.5];
	flashView.alpha = 0;
	[UIView commitAnimations];
	UIAlertView *alert= [[UIAlertView alloc] initWithTitle:nil message:@"Photo saved to your camera roll" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
	[alert show];
	[alert release];
	*/
}

I’ve implemented the saveToCameraRoll method were first five lines do everything you need. It’s quite difficult to discuss it, as long as I never mentioned about Quartz drawing, contexts, layers… But, first line creates the context of our view (UIViewController’s self.view) size. Using that context’s frame (it’s not frame properly speaking) it contents is rendered (2nd line) and saved to an viewImage. 4th line is closing the context as we do not need it anymore, and the 5th line puts viewImage to the Camera Roll.

How about the part in the comment ( /* and */ )? Well it’s my idea how to copy the animation iPhone uses while making the screenshot using home and power buttons. I created the flashView - simple white view of the same size of my self.view most time 100% transparent. When the user decides to create a screenshot using a button I provided in the application, flashView become visible and during half a second it’s transparent again. And the alert to inform what has just happen.

Please remember that above method saves the whole self.view and all it’s subviews. So whatever is a subview of self.view will be saved as well. You can change the alpha property of anything you don’t want to save and reveal what’s behind it, and after screenshot was made bring alpha to normal. While making a screenshot as long as it will be one procedure – one task user won’t see any changes in interface. Example:

-(void)saveToCameraRoll {	topView.alpha = 0;

	UIGraphicsBeginImageContext(self.view.bounds.size);
	[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
	UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	UIImageWriteToSavedPhotosAlbum(viewImage, self, nil, nil); 

	topView.alpha = 1;

}

As always (don’t thank me) I provided an sample project. It alows you to make screenshot of whole iPhone screen (320 x 480 px), only central part (320 x 240 px) and again the whole iPhone screen without button on the bottom.

screenshotssimulator

xcodeproj

Download the project

Ohh, I would forget. One thing more. Making screenshots on simulator is very quick, unfortunately, it takes few more seconds to render everything on real device.

Advertisement

24 comments

  1. If you’re still on the fence: grab your favorite earphones, head down to a Best Buy and ask to plug them into a Zune then an iPod and see which one sounds better to you, and which interface makes you smile more. Then you’ll know which is right for you.

  2. Bernadine498 says:

    I intended to post you the little remark to finally give thanks yet again for your personal splendid things you’ve discussed here. It is really tremendously open-handed with people like you to offer openly what numerous people would have sold for an ebook in order to make some profit on their own, principally considering the fact that you might well have done it if you desired. Those things as well acted like a good way to comprehend other people online have a similar eagerness similar to mine to understand great deal more on the topic of this issue. I think there are a lot more fun occasions ahead for many who looked at your blog.

  3. Nick says:

    I simply had to appreciate you all over again. I am not sure the things that I might have sorted out without those opinions revealed by you directly on my situation. Completely was the intimidating dilemma in my circumstances, however , noticing your expert fashion you treated it took me to weep over gladness. I am thankful for your assistance as well as have high hopes you recognize what a great job you are accomplishing instructing others with the aid of your blog post. Most likely you haven’t come across any of us. http://www.handbagsdreams.com

  4. You can certainly see your enthusiasm within the work you write. The world hopes for even more passionate writers like you who aren’t afraid to mention how they believe. All the time go after your heart.

  5. page rank says:

    Useful info and superb design you got here! I would like to thank you for posting your recommendations and putting the time into the content you publish! Awesome work!

  6. I’m very prepared to look at this approach. This is actually type of manually operated that ought to be presented with and not just all the accidental false information that’s inside the other sorts of blog. Recognize any stating this approach greatest file.

  7. Lab Jobs says:

    Useful facts and fantastic design you got here! I would like to thank you so much for posting your recommendations and putting the time into the posts you publish! Wonderful work!

  8. videncia says:

    The Zune concentrates on being a Portable Media Player. Not a web browser. Not a game machine. Maybe in the future it’ll do even better in those areas, but for now it’s a fantastic way to organize and listen to your music and videos, and is without peer in that regard. The iPod’s strengths are its web browsing and apps. If those sound more compelling, perhaps it is your best choice.

  9. Great site with lots of information.I found you on Yahoo I will be back soon. Thanks

  10. a4866166 says:

    I’ve said that least 4866166 times. The problem this like that is they are just too compilcated for the average bird, if you know what I mean

  11. Thanks so much for sharing this fantastic info! I’m looking forward to see much more posts!

  12. Carol Lane says:

    Hello! I am about to start my own blog and was wondering if you know where the best place to purchase a website url is? I am not even sure if that’s what its called? (I’m new to this) I’m referring to “http://chris-software.com/index.php/2009/04/28/making-screenshots“. Exactly how do I go about acquiring one of these for the website I’m creating? Thankyou

  13. Youre so cool! I dont suppose Ive learn something like this before. So awesome to seek out any individual with some unique ideas on this idea.

  14. First, let me commend your readability on this topic. I am not expert on this theme, but after studying your article, my understanding has developed substantially. Be sure to permit me to snap up your nourish to remain in touch with any potential updates. Great career and will offer it on to supporters and my web site followers.

  15. Satie says:

    Hi…

    This sample looks gr8… But I have one doubt, I am not able to capture the screen while UIView Animation running… can you please tell me how can do that??

  16. Good day! This is kind of off topic but I need some guidance from an established blog. Is it difficult to set up your own blog? I’m not very techincal but I can figure things out pretty fast. I’m thinking about making my own but I’m not sure where to begin. Do you have any points or suggestions? Cheers

  17. Janet Wright says:

    I appreciate you taking the oppurtunity to share this story. Can you tell me any websites that write about topics? I would like to learn more.

  18. Thanks a bunch for sharing this with all of us you actually know what you are talking about! Bookmarked. Please also visit my web site =). We could have a link exchange contract between us!

  19. Scott Rapson says:

    Thanks for this useful code snippet.

    Works a treat in my drawing app Sgraffito. (I will credit you in my help and credits page.)

    Thanks

  20. Mo Kargas says:

    Any idea how to get this to work on MPMovieController instances under iOS3? Can’t seem to take screenshots of a playing movie.

  21. Simon D says:

    Works great – thanks for the terrific post.

  22. Rich says:

    Works great.
    But how do you adjust the size of the screenshot?
    I want to crop the screenshot to a smaller size.

    • Chris says:

      You can make a transform (scale) to 50% for example right before you start the image context and transform it back – either to 100% or transform identity after you finished with screenshot.

      Regards
      Chris

  23. Edgard Rodriguez says:

    For Me work Like a Charm.

    thanks bro

Leave a Reply