iPhone 应用里实现截屏功能的代码
2015-11-19 16:25
501 查看
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!"); } } }
相关文章推荐
- Eclipse用法和技巧十八:减少不必要的输入
- C++ stringstream介绍,使用方法与例子
- thinkphp命名空间
- golang 大文件分割
- 部署Java在服务器端的EJB组件的方法
- 关于matlab中pcolor显示图片时的shading设置问题
- Java动态代理学习
- java读取csv数据到list缓存,并对list集合分组统计结果
- python 函数参数的传递(参数带星号的说明)
- C与Lua互相调用的时候,栈变化分析
- Eclipse用法和技巧十七:覆盖父类方法
- C++ 09原型模式
- eclipse开发用ant生成myeclipse那样的执行文件夹
- python eventlet模块
- Struts2 Ajax---Action传值到JSP页面 Json格式
- eclipse下git不能拉取错误解决小记
- Eclipse用法和技巧十六:自动添加未实现方法2
- Google breakpad
- C语言用一维数组打印杨辉三角(原:无意中想到)
- 程序设计: 猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。(C#控制台程序)