iOS应用截屏
iPhone自从推出后就自带了截屏功能,简单而易用,所以应用就没什么截屏的需求了,不过有些时候我们还是会遇到这个需求。比如,我们开发了一个播放器,用openGL进行video render,此时直接截屏有可能有OSD叠加内容,所以希望能截完全是视频的帧,这时就需要应用自己来实现了。
从应用角度看,虽说都是截屏,但用不用openGL是不同的,因为openGL是直接写GPU frame buffer的,如果我们是直接用UIController来用做的界面:
- - (void)snapshotScreen
- {
- // check the retina screen
- if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]){
- UIGraphicsBeginImageContextWithOptions(self.view.window.bounds.size, NO, [UIScreen mainScreen].scale);
- } else {
- UIGraphicsBeginImageContext(self.view.window.bounds.size);
- }
- [self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- NSData * data = UIImagePNGRepresentation(image);
- NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *filename = [[path objectAtIndex:0] stringByAppendingPathComponent:@"foo.png"];
- [data writeToFile:filename atomically:YES];
- }
这个代码前面部分是检查是否是retina屏幕的,因为iPhone都是retina的屏幕,但iPod有非retina的屏幕;最后一部分是把image存到应用的document目录下。
如果要截openGL的内容,那么上面的代码就不能用了,思路是用openGL的API,glReadPixels去读出内容来。代码如下:
- - (void)getGLScreenShot {
- int w = self.bounds.size.width;
- int h = self.bounds.size.height;
- NSInteger myDataLength = w * h * 4;
- // allocate array and read pixels into it.
- GLubyte *buffer = (GLubyte *) malloc(myDataLength);
- glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
- // gl renders "upside down" so swap top to bottom into new array.
- // there's gotta be a better way, but this works.
- GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
- for(int y = 0; y < h; y++) {
- for(int x = 0; x <w * 4; x++) {
- buffer2[(h -1 - y) * w * 4 + x] = buffer[y * 44 * w + x];
- }
- }
- // make data provider with data.
- CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
- // prep the ingredients
- int bitsPerComponent = 8;
- int bitsPerPixel = 32;
- int bytesPerRow = 44 * w;
- CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
- CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
- CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
- // make the cgimage
- CGImageRef imageRef = CGImageCreate(w, h, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
- // then make the uiimage from that
- UIImage *myImage = [UIImage imageWithCGImage:imageRef];
- UIImageWriteToSavedPhotosAlbum(myImage, self, @selector(GLImage:didFinishSavingWithError:contextInfo:), buffer2);
- CGImageRelease(imageRef);
- CGDataProviderRelease(provider);
- CGColorSpaceRelease(colorSpaceRef);
- free(buffer);
- }
这段代码是抓下了openGL渲染的一个GLView的所有内容,因为openGL读出的内容是颠倒的,所以习惯上需要颠倒一下,中间的两层for循环就是这个用途,当然,这个写法效率不高,不过一时也没找到什么高效的方法,就先用一下。
这段代码里面释放了buffer的内存,但没有释放buffer2的内存。因为图片的存储是异步的,所以是在存完图片之后,调用@selector(GLImage:didFinishSavingWithError:contextInfo:)这个方法进行清理,通过这个回调,在UI上也可以做一些限制,防止用户连续快速的截屏导致系统负载过重。
顺便说一下,这里把存下的图片按照习惯放到了图片库之中。
在iOS7之后,UIView有了UISnapshotting的category,这个真是大大的方便了我们截屏的实现,因为它既可以截屏普通的UIController,也可以截屏openGL的内容,
- // only support iOS7 or Above
- - (void)snapshotScreenWithGL
- {
- CGSize size = videoView.bounds.size;
- UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
- CGRect rec = CGRectMake(videoView.frame.origin.x, videoView.frame.origin.y, videoView.bounds.size.width, videoView.bounds.size.height);
- [self.view drawViewHierarchyInRect:rec afterScreenUpdates:YES];
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- NSData * data = UIImagePNGRepresentation(image);
- NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *filename = [[path objectAtIndex:0] stringByAppendingPathComponent:@"foo.png"];
- [data writeToFile:filename atomically:YES];
- }
综合起来看,iOS7之后,苹果考虑到了用户这方面的需求,提供了API,可以比较方便的实现功能。iOS7之前,需要自己手动实现,根据是否使用了openGL代码有所不同。
iOS应用截屏的更多相关文章
- IOS上架截屏 屏幕快照
IOS上架截屏,屏幕快照,4种屏幕尺寸,每种尺寸5张软件功能截图. 大小等于对应设备的屏幕的像素大小.使用模拟器,command +s截图就可以了虚拟机里的手机截屏就保存在mac 桌面上了.jpg,p ...
- iOS 模拟器截屏快捷键
iOS 模拟器截屏快捷键: cmd+S
- IOS中截屏的实现,很简易的方法
// 添加QuartzCore.framework库 #import <QuartzCore/QuartzCore.h> -(void) screenShot { // 截屏 UIGrap ...
- iOS手机截屏使用
.截屏 保存 .data //登录成功进行截屏 //截取屏幕大小 UIGraphicsBeginImageContext([[UIScreen mainScreen]bounds].size); [s ...
- iOS实现截屏 并合适保存
本文转载至:http://blog.csdn.net/zeng11088/article/details/8664510 分类: UIImageView2013-03-12 16:42 122人阅读 ...
- iOS 手机截屏
百度地图自带截图功能,可以截取路线列表,保存到本地.可是对比发现截下来的图片并不是app中看到的那样,截图中头部加入了搜索的起点和终点,每段路程的详细站点都已展开,而且图片会根据路线的长短自动判断图片 ...
- iOS屏幕截屏
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...
- ios 代码截屏模糊问题解决办法
我们常用的截图方法如下所示: //尺寸是按照 UIGraphicsBeginImageContext(CGSizeMake(, )); //currentView 当前的view 创建一个基于位图的图 ...
- ios实现截屏(转)
-(UIImage*) makeImage { UIGraphicsBeginImageContext(self.view.bounds.size); [self.view.layer rende ...
随机推荐
- emmet常用指令组合
emmet的应用 1.生成html(需要先将文件命名为.html后缀) !+tab,html:5+tab 2.生成meta utf meta:utf+tab 3.生成meta viewpo ...
- 在 Spark 中使用 IPython Notebook
本文是从 IPython Notebook 转化而来,效果没有本来那么好. 主要为体验 IPython Notebook.至于题目,改成<在 IPython Notebook 中使用 Spark ...
- 分享几道Java线程面试题
不管你是新程序员还是老手,你一定在面试中遇到过有关线程的问题.Java语言一个重要的特点就是内置了对并发的支持,让Java大受企业和程序员的欢迎.大多数待遇丰厚的Java开发职位都要求开发者精通多线程 ...
- 堆 Heap
2018-03-01 20:38:34 堆(Heap)是可以用来实现优先的队列的数据结构,而不是堆栈. 若采用数组或者链表实现优先队列 若采用树的结构 如果采用二叉搜索树,那么每次删除,比如删除最大值 ...
- 基于centos的docker安装
1. 安装需求 内核版本3.10以上 Centos 7以上 64位版本 2. 使用root登录或者具有sudo权限 3. 确保系统是最新的 yum update 4. 添加yum源 tee /etc/ ...
- 【css】25个漂亮的响应式布局的web设计【转】
响应的web设计的做法是提高用户的浏览质量,并在不同设备上能够完美的浏览使用,就像大前端推出的D7主题.看看下面美丽的响应的网站布局,通过本文你会在以后的设计中找到响应的web设计的灵感. 1.Mar ...
- redhat9安装gcc(转)
原文链接:http://blog.chinaunix.net/uid-20260767-id-118036.html 第一种方法: 相信现在还有不少人在用经典的RedHat9,毕竟他是完全免费的红帽L ...
- GreenPlum的Primary和Mirro切换恢复
gp节点出现了acting as primary change tracking错误,判断是节点primary和mirror发生了切换 1.没有配置gp的日志,无法获取为什么切换了,待会儿看看默认日志 ...
- 在Vim中使用gtags
之前一直使用vim+ctags+cscope来弄c的代码,最近看同事使用gtags,觉得在搜索方面要高级很多,网上大多都是emacs+gtags的资料,而vim的则比较少,这里搞通了之后,做个记录. ...
- Popular HashMap and ConcurrentHashMap Interview Questions
http://howtodoinjava.com/core-java/collections/popular-hashmap-and-concurrenthashmap-interview-quest ...