ios6下cocos2d截屏失效问题处理

本文详细介绍了在iOS6系统下使用Cocos2d框架进行游戏开发时遇到的截屏全黑问题,并提供了两种解决方案。首先,通过将GLView的preserveBackbuffer属性设置为YES来解决此问题;其次,介绍了一个自定义截屏函数,该函数通过创建临时渲染纹理并填充白色背景来实现正确的截图效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

相信使用cocos2d官方论坛提供的截屏方法,或者其他使用openGL方式截屏的同志们,会发现在iOS6系统的真机上截图为全黑色,但是在其他版本系统的真机上没问题,而且在模拟器上也没问题,包括iOS6的模拟器。网上狂搜了一把,解决方案如下:

第一种:设置GLView的preserveBackbuffer属性值为YES:

在AppDelegate.m文件中找到:

[csharp]  view plain copy
  1. EAGLView *glView = [EAGLView viewWithFrame:[window bounds]  
  2.                                pixelFormat:kEAGLColorFormatRGB565   // kEAGLColorFormatRGBA8  
  3.                                depthFormat:0                        // GL_DEPTH_COMPONENT16_OES  
  4.                     ];  

将其修改为:

[csharp]  view plain copy
  1. EAGLView *glView = [EAGLView viewWithFrame:[window bounds]  
  2.                                pixelFormat:kEAGLColorFormatRGB565   // kEAGLColorFormatRGBA8  
  3.                                depthFormat:0                        // GL_DEPTH_COMPONENT16_OES  
  4.                                preserveBackbuffer:YES   
  5.                                sharegroup:nil   
  6.                                multiSampling:NO   
  7.                                numberOfSamples:0   
  8.                     ];  

第二种:使用以下一种截屏函数:

[csharp]  view plain copy
  1. +(UIImage*) makeaShot  
  2. {  
  3.     [CCDirector sharedDirector].nextDeltaTimeZero = YES;  
  4.     CGSize winSize = [CCDirector sharedDirector].winSize;  
  5.     CCLayerColor* whitePage = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 0) width:winSize.width height:winSize.height];  
  6.     whitePage.position = ccp(winSize.width/2, winSize.height/2);  
  7.     CCRenderTexture* rtx = [CCRenderTexture renderTextureWithWidth:winSize.width height:winSize.height];  
  8.     [rtx begin];  
  9.     [whitePage visit];  
  10.     [[[CCDirector sharedDirector] runningScene] visit];  
  11.     [rtx end];  
  12.     return [rtx getUIImageFromBuffer];  
  13. }  

[html]  view plain copy
  1. -(UIImage*) screenshotWithStartNode:(CCNode*)startNode  
  2. {  
  3.     [CCDirector sharedDirector].nextDeltaTimeZero = YES;  
  4.     CGSize winSize = [CCDirector sharedDirector].winSize;  
  5.     CCRenderTexture* rtx =   
  6.     [CCRenderTexture renderTextureWithWidth:winSize.width   
  7.                                  height:winSize.height];  
  8.     [rtx begin];  
  9.     [startNode visit];  
  10.     [rtx end];  
  11.     return [rtx getUIImage];  
  12. }  
  13.   
  14. CCScene *scene = [[CCDirector sharedDirector] runningScene];  
  15. CCNode *n = [scene.children objectAtIndex:0];  
  16. UIImage *img = [self screenshotWithStartNode:n];  
http://blog.youkuaiyun.com/wangqiuyun/article/details/8126271
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值