除了直接使用手机按键截屏,在App中我们也可以通过代码去实现截屏功能。以下是我的工程里封装的截屏方法,在此记录一下。
1.使用drawViewHierarchyInRect方法截取当前屏幕
[view drawViewHierarchyInRect:view.frame afterScreenUpdates:YES];
2.把截取的当前UIView转成UIImage
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
3.图片转成NSData二进制,保存到沙盒中
NSData * imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:fullPathToFile atomically:NO];
4.读取文件下的图片
完整示例代码
1.自定义一个继承于NSObject的模型类PubSysFunc
//截屏方法
+ (void)screenShotAction:(UIView *)view{
//使用respondsToSelector方法判断,防止异常时崩溃
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]){
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
} else {
UIGraphicsBeginImageContext(view.bounds.size);
}
//截取当前屏幕
[view drawViewHierarchyInRect:view.frame afterScreenUpdates:YES];
//视图转成UIImage
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//保存图片到沙盒
NSData * imageData = UIImagePNGRepresentation(image);
//自定义文件存储路径
NSString *folderPath = [PubSysFunc createUserDataFolder:@"Images"];
UInt64 time = [[NSDate date] timeIntervalSince1970]*1000;
NSString *imageName = [NSString stringWithFormat:@"%llu.png",time];
NSString * fullPathToFile = [folderPath stringByAppendingPathComponent:imageName];
//写入文件
[imageData writeToFile:fullPathToFile atomically:NO];
}
//在UserData目录下创建文件夹
//#define USERDATAPATH @"xxx/xxx/UserData"
+ (NSString *)createUserDataFolder:(NSString *)folderName{
//Document目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSString *userPath = [path stringByAppendingPathComponent:USERDATAPATH];
NSString *folderPath = [userPath stringByAppendingPathComponent:folderName];
NSFileManager *fm = [NSFileManager defaultManager];
if(![fm fileExistsAtPath:folderPath]) {
//创建文件夹
if (![[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil]) {
return nil;
}
}
return folderPath;
}
//得到该路径下的所有指定后缀名的文件
//这里传入extensionName值用于区分是图片还是视频
+ (NSArray *)getAllFilePathWithFolderPath:(NSString *)path extension:(NSString *)extensionName{
if (path == nil) {
//目标文件不存在
return nil;
};
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDirectoryEnumerator *myDirectoryEnumerator = [fileManager enumeratorAtPath:path];
//用来存目录名字的数组
NSMutableArray *filePathArray = [[NSMutableArray alloc]init];
NSString *file = nil;
//遍历当前目录
while((file=[myDirectoryEnumerator nextObject])){
if([[file pathExtension] isEqualToString:extensionName]){
//存到数组
[filePathArray addObject:[path stringByAppendingPathComponent:file]];
}
}
return filePathArray;
}
2.在VC中调用截屏方法
//#define KEYWIN [[UIApplication sharedApplication] keyWindow]
- (void)screenshot{
[PubSysFunc screenShotAction:KEYWIN];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"保存成功" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:cancelAction1];
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[delegate.window.rootViewController presentViewController:alert animated:YES completion:nil];
}
3.在VC中读取文件
- (void)getDataSource
{
NSString *path = [PubSysFunc getDestDocPath:[NSString stringWithFormat:@"%@/Images",USERDATAPATH]];
NSArray *nameList = [PubSysFunc getAllFilePathWithFolderPath:path extension:@"png"];
//nameList即为USERDATAPATH路径下获取到的图片
}
网友评论