Monday, 23 December 2013

How to take screen shots programmatically ??

Hi.
In this tutorial ,am talking about how we can take screen shots programmatically  instead of "Home+Power" button combination on your device.

It's very simple and i have only two files "ViewController.h" and "ViewController.m"
and you have to import "#import <QuartzCore/QuartzCore.h>".

here we go ..

in my ViewController.h file

i have one method for UIButton.



#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
- (IBAction)openMail:(id)sender;
@end




In my ViewController.m file


#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
{
    NSArray *paths;
    NSString *filename,*newPath;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
        [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)takescreenshotes{
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);

    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData * data = UIImagePNGRepresentation(image);

    filename=@"latest.png"; //create a custome file name for your screen shots
 
    paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    newPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:filename];
   
    [data writeToFile:newPath atomically:YES]; //Path to store the screen shots
 
    UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:data], nil, nil, nil);
}

- (IBAction)openMail:(id)sender
{
    [self takescreenshotes];

 }

 
@end


and my xib looks like








You can download the code from here













No comments:

Post a Comment