15 Oct 2015 0 comments

Unite is a conference that is run by Unity Technologies to promote their key product, the Unity gaming engine. It isn’t so much as a conference, but rather an experience. With a keynote being pre...

Read More
15 Oct 2015 0 comments

While I worked on creating the iOS Animations by Tutorials video series and the three editions of the iOS Animations by Tutorials book I got a lot of ideas how the existing anim...

Read More
15 Oct 2015 0 comments

Happy Wednesday – it’s time for our 3rd new book release as part of the iOS 9 Feast! Today, Aaron, Saul, Matthew, Pietro and I are happy to announce that Core Data by Tutorials Second E...

Read More
15 Oct 2015 0 comments

In this getting started with Core Data tutorial, you’ll write your very first Core Data app using Swift 2.0. You’ll see how easy it is to get started with all the resources provided in Xcode, fro...

Read More
15 Oct 2015 0 comments

When you create a Core Data app, you design an initial data model for your app. However, after you ship your app inevitably you’ll want to make changes to your data model. What do you do then – you...

Read More
15 Oct 2015 0 comments

As part of this year’s iOS 9 Feast, we are releasing a new video tutorial series every Thursday. This week, we are happy to release a brand new video tutorial series – Introducing Custom Cont...

Read More
15 Oct 2015 0 comments

There’s been a big hole in Spotlight on iOS for a long time. Although users can use it to find you app, they can’t see inside it — to all the content that they really care about. Currentl...

Read More
15 Oct 2015 0 comments

Storyboards have been around since iOS 5 and have received lots of upgrades and new features since then, including unwind segues for reverse navigation, universal storyboards for both iPhone and iP...

Read More

Latest Posts:

Programmatically Getting a Screencapture of any UIView


Introduction

Hey guys. This post is in response to a comment made in my last post about NSTimers. Techy asked if we could do a post on how to take screenshots programmatically using the iPhone SDK. For this minor project we will be creating a small web browser application that will start at www.google.com, and have a button on a toolbar at the bottom to take a picture of the web view and save it into you photo library. Lets take a look.

Screenshots

Source

You can get the source here: Screenshot Project

Steps

Step 1

Create a view based application in xCode. Call it whatever.

Step 2

In the header for your view controller declare the following:
#import
 
@interface ScreenCaptureViewController : UIViewController
{
 IBOutlet UIWebView *webview;
}
 
@property (nonatomic, retain) IBOutlet UIWebView *webview;
 
-(IBAction)captureScreen:(id)sender;
 
@end

Step 3

Open up your view controller’s XIB file.
  • Drag in a UIToolbar and put it at the bottom of the screen.
  • Drag in a UIToolbarButtom and name it appropriately.
  • Drag in a flexible space holder on either side of the button to center it.
  • Drag in a UIWebView to take up the rest of the screen above the toolbar.
  • Connect the UIWebView to our webview IBOutlet.
  • Connect out UIToolbarButton to our IBAction catureScreen:(id)sender.

Step 4

Uncomment out the viewDidLoad method and use the following code:
- (void)viewDidLoad
{
    [super viewDidLoad];
 
    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
}

Step 5

All the is left to do is create the IBAction captureScreen. This is the code for the method, place it in the main file for your view controller.
-(IBAction)captureScreen:(id)sender
{
 UIGraphicsBeginImageContext(webview.frame.size);
 [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
 UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
}

Conclusion

So that is it. You can use this general code to take a screenshot of any UIView subclass. All you need to do is replace webview.frame.size in the third line with foobar.frame.size where foobar is any UIView subclass. Hope this answered your question Techy. Happy Coding.
Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment