相信使用cocos2d官方论坛提供的截屏方法,或者其他使用openGL方式截屏的同志们,会发现在iOS6系统的真机上截图为全黑色,但是在其他版本系统的真机上没问题,而且在模拟器上也没问题,包括iOS6的模拟器。网上狂搜了一把,解决方案如下:
第一种:设置GLView的preserveBackbuffer属性值为YES:
在AppDelegate.m文件中找到:
- EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
- pixelFormat:kEAGLColorFormatRGB565 // kEAGLColorFormatRGBA8
- depthFormat:0 // GL_DEPTH_COMPONENT16_OES
- ];
将其修改为:
- EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
- pixelFormat:kEAGLColorFormatRGB565 // kEAGLColorFormatRGBA8
- depthFormat:0 // GL_DEPTH_COMPONENT16_OES
- preserveBackbuffer:YES
- sharegroup:nil
- multiSampling:NO
- numberOfSamples:0
- ];
第二种:使用以下一种截屏函数:
- +(UIImage*) makeaShot
- {
- [CCDirector sharedDirector].nextDeltaTimeZero = YES;
- CGSize winSize = [CCDirector sharedDirector].winSize;
- CCLayerColor* whitePage = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 0) width:winSize.width height:winSize.height];
- whitePage.position = ccp(winSize.width/2, winSize.height/2);
- CCRenderTexture* rtx = [CCRenderTexture renderTextureWithWidth:winSize.width height:winSize.height];
- [rtx begin];
- [whitePage visit];
- [[[CCDirector sharedDirector] runningScene] visit];
- [rtx end];
- return [rtx getUIImageFromBuffer];
- }
- -(UIImage*) screenshotWithStartNode:(CCNode*)startNode
- {
- [CCDirector sharedDirector].nextDeltaTimeZero = YES;
- CGSize winSize = [CCDirector sharedDirector].winSize;
- CCRenderTexture* rtx =
- [CCRenderTexture renderTextureWithWidth:winSize.width
- height:winSize.height];
- [rtx begin];
- [startNode visit];
- [rtx end];
- return [rtx getUIImage];
- }
- CCScene *scene = [[CCDirector sharedDirector] runningScene];
- CCNode *n = [scene.children objectAtIndex:0];
- UIImage *img = [self screenshotWithStartNode:n];