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

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