IOS 截屏单例代码
时间:2014-06-22 21:32:00
收藏:0
阅读:315
一. IOS 的截取全屏代码为:
二. 截取指定区域:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size,self.view.opaque,0.0);
[self.myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage*theImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData*theImageData=UIImageJPEGRepresentation(theImage,1.0 );//you can use PNG too
[theImageData writeToFile:@"example.jpeg" atomically:YES];
三.截屏代码的单例
ScreenShot.h文件
ScreenShot.m文件
四.使用:
1.在ViewController中引入头文件ScreenShot.h
//截屏并保存 [ScreenShot sharedScreenShot] getScreenImage];
UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow]; //1.截屏 UIGraphicsBeginImageContext(screenWindow.frame.size); [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
二. 截取指定区域:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size,self.view.opaque,0.0);
[self.myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage*theImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData*theImageData=UIImageJPEGRepresentation(theImage,1.0 );//you can use PNG too
[theImageData writeToFile:@"example.jpeg" atomically:YES];
三.截屏代码的单例
ScreenShot.h文件
#import <Foundation/Foundation.h> @interface ScreenShot : NSObject ///截屏图片存放位置 @property (nonatomic,copy) NSString *filePath; /* 单例函数 */ + (id)sharedScreenShot; /* 截屏,并写入内存 */ -(void)getScreenImage; @end
ScreenShot.m文件
#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
四.使用:
1.在ViewController中引入头文件ScreenShot.h
//截屏并保存 [ScreenShot sharedScreenShot] getScreenImage];
2. 获得图片
NSString *shotFilePath=((ScreenShot *)[ScreenShot sharedScreenShot]).filePath; NSData *shotImageData = [NSData dataWithContentsOfFile:shotFilePath]; UIImage *shotImage = [UIImage imageWithData:shotImageData];
评论(0)