iOS 截屏功能的实现

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}

 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    NSLog(@"开始截屏");

 

    [self cutterViewToDocument];

}

 

- (void)cutterViewToDocument

{

         UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];

    

         UIGraphicsBeginImageContext(screenWindow.frame.size);

         [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];

         UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

    

       NSData *screenShotPNG = UIImagePNGRepresentation(screenShot);

         NSError *error = nil;

     BOOL  isSuccess=  [screenShotPNG writeToFile:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"screen.png"] options:NSAtomicWrite error:&error];

    NSLog(@"%@",NSHomeDirectory());

    if (isSuccess) {

        NSLog(@"截屏成功");

    }else{

    

        NSLog(@"截屏失败");

    }

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

 

posted @ 2015-07-10 14:35  曦曦果果  阅读(494)  评论(0编辑  收藏  举报