iOS 6上的Cocos2D 2.0截图

7
我有一个应用程序,可以截取屏幕并保存到文件中。我已经实现了这个功能,并将应用程序放在商店中。今天,我下载了iOS 6,但我使用的方法不再起作用。我尝试了所有我知道的方法来使它工作,还在Google上搜索到了这个链接:http://www.cocos2d-iphone.org/forum/topic/37809?replies=22#post-180983。用户似乎认为这个方法适用于iOS 5,但我在iOS 6上测试后发现仍然会产生黑色的截图。由于我不是Cocos2D的专家,所以无法确定这位作者代码中的问题。作者在github上提供了一个样例项目,但即使他的项目在iOS 6上也会产生黑色的截图。有什么线索吗?谢谢。

我对cocos2D中的问题没有直接的解决方案,但是我可以分享一下,我在iOS6和直接OpenGL屏幕截图方面遇到了相同的问题。这个问题并不是特定于cocos2D的。 - TakMan
1
这与问题为什么在iOS 6.0中,glReadPixels()在此代码中失败?有关,原因是在将像素从帧缓冲区读取出来后,iOS现在不再返回任何内容。苹果公司已经警告这是不支持的行为一段时间了,看起来他们终于采取了行动。 - Brad Larson
我明白了,谢谢。幸运的是,Ben 给出的解决方案可行。顺便说一句,Brad,我们在 iTunesU 上想念你的教程... - Duck
抱歉,问题中发布的链接似乎现在无效了... - Krishna Raj Salim
4个回答

33

我不确定GitHub版本做了什么,但这段代码将会截取屏幕截图,我刚在iOS 6上测试过,它正常工作。

+(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 = [AppController screenshotWithStartNode:n];

谢谢,但据我所见,这将生成不在视网膜大小上的屏幕截图,因为您正在定义纹理的大小为winSize而不是winSizeInPictures... - Duck
1
CCRenderTexture会为您处理这个问题。它的纹理大小以点为单位而不是像素。这段代码确实可以在Retina尺寸下截屏,我刚刚测试过了。 - Ben Trengrove
我的奥斯卡奖颁给 @BenTrengrove :) 谢谢你,伙计,你救了我的一天 ;) - yatanadam
这是cocos2d 2.0上CCRenderTexture的一个方法。 - Ben Trengrove
我已经实现了代码,但是截图不是很清晰,有点模糊。 - oopology

2
这里适用于Cocos2d V3。
+(UIImage*) screenshotWithStartNode:(CCNode*)startNode
{
    [CCDirector sharedDirector].nextDeltaTimeZero = YES;

    CGSize viewSize = [[CCDirector sharedDirector] viewSize];
    CCRenderTexture* rtx =
    [CCRenderTexture renderTextureWithWidth:viewSize.width
                                     height:viewSize.height];
    [rtx begin];
    [startNode visit];
    [rtx end];

    return [rtx getUIImage];
}

0

顶部答案适用于iPad(我已在iPad v1到4上进行了测试)。

它不适用于实际设备:iPhone5,iOS7。

但是,它适用于此类iPhone 5的模拟器!

这种不一致性让我发疯!


0

以上两个答案在Cocos2d 3.2.1中不起作用。

这里是适用于Cocos2d 3.2.1及以上版本的解决方案。

-(UIImage*) takePresentScreenshot
{
    [CCDirector sharedDirector].nextDeltaTimeZero = YES;

    CGSize size = [[CCDirector sharedDirector] viewSize];
    CCRenderTexture *renderTxture = [CCRenderTexture renderTextureWithWidth:size.width
                                                                  height:size.height];
    [renderTxture begin];
    [[[CCDirector sharedDirector] runningScene] visit];
    [renderTxture end];

    return [renderTxture getUIImage];
}

UIImage* screenshot = [self takePresentScreenshot];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接