7

I have an application that takes a screenshot of a scene and saves it to a file. I have this working and the application is on the store. Today, I have downloaded iOS 6 and the method I am using is not working anymore. I tested all I know to make it work, googled around and found this:

http://www.cocos2d-iphone.org/forum/topic/37809?replies=22#post-180983

Users seem to agree that this is working on iOS 5, but I have tested this on iOS 6 and it is producing black screenshots.

I am not a specialist in Cocos2D so, I cannot say exactly what is wrong with this guy's code. the author has a sample project on github and even his project is producing black screenshots on iOS 6.

Any clues? Thanks.

thanks

Ben Trengrove
  • 8,191
  • 3
  • 40
  • 58
Duck
  • 34,902
  • 47
  • 248
  • 470
  • I don't have a direct solution for the problem in cocos2D, but I can share that I am having the same issue with iOS6 and a direct OpenGL screen grab. The problem is not cocos2D specific. – TakMan Sep 27 '12 at 17:57
  • 1
    This is related to the question [Why is glReadPixels() failing in this code in iOS 6.0?](http://stackoverflow.com/questions/12528365/why-is-glreadpixels-failing-in-this-code-in-ios-6-0), and is due to the fact that iOS now returns nothing when reading pixels from the framebuffer after it has been presented to the screen. Apple has warned that this was unsupported behavior for a while, and it looks like they've finally acted on that. – Brad Larson Sep 29 '12 at 03:55
  • I see. Thanks. Fortunately the solution given by Ben is working. BTW, Brad, we miss your tutorials at iTunesU... – Duck Sep 29 '12 at 04:32
  • sorry, the link posted in the question seems not valid now... – Krishna Raj Salim Nov 16 '13 at 20:44

4 Answers4

33

I am not sure what the GitHub version does but this code will take a screenshot and I just tested it on iOS 6 and it works fine.

+(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];
}

You can call it like this

CCScene *scene = [[CCDirector sharedDirector] runningScene];
CCNode *n = [scene.children objectAtIndex:0];
UIImage *img = [AppController screenshotWithStartNode:n];
Ben Trengrove
  • 8,191
  • 3
  • 40
  • 58
  • Thanks but as far as I see this will generate screenshots that are not on the retina size, as you are defining the size of the texture to be winSize and not winSizeInPictures... – Duck Sep 13 '12 at 22:28
  • 1
    CCRenderTexture handles that for you. It's texture size is in Points not Pixels. This code does indeed take screenshots at Retina size, I just tested it. – Ben Trengrove Sep 13 '12 at 22:33
  • anddd my oscar goes toooo.. @BenTrengrove :) Thanks guy you saved my day ;) – yatanadam Dec 22 '12 at 15:22
  • It is a method of CCRenderTexture on cocos2d 2.0. – Ben Trengrove May 07 '13 at 22:20
  • I have implemented the code, but the screenshots are not very clear, they have a little blur. – oopology Sep 19 '14 at 16:26
2

This here works for 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];
}
Herman
  • 54
  • 2
0

Above 2 answers not worked in Cocos2d 3.2.1

Here is Solution for 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];
Guru
  • 21,652
  • 10
  • 63
  • 102
0

The top answer works for iPads (I have tested on iPads v1 thru 4).

It does NOT work for the actual device: iPhone5, iOS7.

However, it does work for simulators of such an iPhone 5!

That inconsistency is driving me crazy!

jvschmitt
  • 61
  • 5