截屏并保存
2015-09-30 13:13
369 查看
UIView *view = [[[[[UIApplication sharedApplication] windows] objectAtIndex:1] subviews] lastObject];//获得某个window的某个subView NSInteger index = 0;//用来给保存的png命名 for (UIView *subView in [view subviews]) {//遍历这个view的subViews if ([subView isKindOfClass:NSClassFromString(@"UIImageView")] || [subView isKindOfClass:NSClassFromString(@"UIThreePartButton")]) {//找到自己需要的subView //支持retina高分的关键 if(UIGraphicsBeginImageContextWithOptions != NULL) { UIGraphicsBeginImageContextWithOptions(subView.frame.size, NO, 0.0); } else { UIGraphicsBeginImageContext(subView.frame.size); } //获取图像 [subView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); //保存图像 NSString *path = [NSHomeDirectory() stringByAppendingFormat:@"/%d.png",index]; if ([UIImagePNGRepresentation(image) writeToFile:path atomically:YES]) { index += 1; NSLog(@"Succeeded!"); } else { NSLog(@"Failed!"); } } } 在真机上可以用以下私有函数截屏,不过苹果会reject用了这个函数的软件。 CGImageRef UIGetScreenImage(); @interface UIImage (ScreenImage) + (UIImage *)imageWithScreenContents; @end @implementation UIImage (ScreenImage) + (UIImage *)imageWithScreenContents { CGImageRef cgScreen = UIGetScreenImage(); if (cgScreen) { UIImage *result = [UIImage imageWithCGImage:cgScreen]; CGImageRelease(cgScreen); return result; } return nil; } @end
相关文章推荐
- 使用FlowDroid生成Android应用程序的函数调用图
- 数据库规范
- 函数返回局部变量
- 6_11:strftime 打印系统时间
- Oracle多行函数/组函数
- xss攻击
- 项目四——建设双链表算法库
- LeetCode (22): Generate Parentheses
- Cassandra 常见错误索引
- Matlab中显示法线方向 display normal map in matlab
- maven install 读取jar包时出错;error in opening zip file
- Oracle Study之--Oracle 11g RAC故障(Failed to create or upgrade OLR)
- freeswitch语音信箱(Voice Mail)功能调试
- UIImageView添加边框和阴影
- WebSockets
- 华为-on练习--重复的字符过滤
- 从零自学Hadoop(05):Ambari
- 毕设帮(5188.help)开发日志2——我的构想有市场吗?
- 毕设帮(5188.help)开发日志2——我的构想有市场吗?
- 创建可以重复利用的QML Component--Podcast播放器代码实例