static int ScreenshotIndex=0;
//判断是否是retina屏
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]){
// 开启图像上下文
UIGraphicsBeginImageContextWithOptions(self.view.window.bounds.size, NO, [UIScreen mainScreen].scale);
} else {
// 开启图像上下文
UIGraphicsBeginImageContext(self.view.window.bounds.size);
}
// 将当前视图图层渲染到当前上下文
[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
// 从当前上下文获取图像
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// 关闭图像上下文
UIGraphicsEndImageContext();
// 保存图片到相册
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * pictureName= [NSString stringWithFormat:@"image_%d.png",ScreenshotIndex];
self.savedImagePath = [documentsDirectory stringByAppendingPathComponent:pictureName];
NSLog(@"截屏路径打印: %@", self.savedImagePath);
NSData * data = UIImagePNGRepresentation(image);
//保存照片到沙盒目录
[data writeToFile:self.savedImagePath atomically:YES];
ScreenshotIndex++;