标签:style class code ext color com
一. IOS 的截取全屏代码为:UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow]; //1.截屏 UIGraphicsBeginImageContext(screenWindow.frame.size); [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
#import <Foundation/Foundation.h> @interface ScreenShot : NSObject ///截屏图片存放位置 @property (nonatomic,copy) NSString *filePath; /* 单例函数 */ + (id)sharedScreenShot; /* 截屏,并写入内存 */ -(void)getScreenImage; @end
#import "ScreenShot.h" ///截屏图片 #define ScreenShotImage @"screenshot.png" @implementation ScreenShot @synthesize filePath=_filePath; - (id)init { if (self = [super init]) { _filePath=[self documentsPathForFileName:ScreenShotImage]; } return self; } #pragma mark -单例 + (id)sharedScreenShot { static ScreenShot *screenShot = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ screenShot = [[self alloc] init]; }); return screenShot; } #pragma mark 通过文件名获得路径 - (NSString *)documentsPathForFileName:(NSString *)name { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *documentsPath = [paths objectAtIndex:0]; return [documentsPath stringByAppendingPathComponent:name]; } #pragma mark - 截屏 -(void)getScreenImage{ UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow]; //1.截屏 UIGraphicsBeginImageContext(screenWindow.frame.size); [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); //2.保存 NSData *screenshotPNG = UIImagePNGRepresentation(screenshot); NSError *error = nil; //3.写入内存 [screenshotPNG writeToFile:_filePath options:NSAtomicWrite error:&error]; } @end
2. 获得图片
NSString *shotFilePath=((ScreenShot *)[ScreenShot sharedScreenShot]).filePath; NSData *shotImageData = [NSData dataWithContentsOfFile:shotFilePath]; UIImage *shotImage = [UIImage imageWithData:shotImageData];
标签:style class code ext color com
原文地址:http://blog.csdn.net/wildcatlele/article/details/32160485