码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 截屏功能的实现

时间:2015-07-10 14:51:37      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

#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

 

iOS 截屏功能的实现

标签:

原文地址:http://www.cnblogs.com/woaixixi/p/4635665.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!