2

Is there an efficient way to do this? currently i use

[renderTexture begin];
[self.view visit];
[renderTexture end];

but app jitters because of it.

I read I can copy contents of frame buffer for this. Is that true? Can someone guide me how to do that.

anuj
  • 1,010
  • 2
  • 11
  • 26

3 Answers3

0

I used a method that worked for me. It does a screenshot, and then selects a frame of it. You can change it, so it takes a screenshot from the whole screen. Here's the code.

-(UIImage*) takeScreenShot
{
[CCDirector sharedDirector].nextDeltaTimeZero = YES;
CGSize winSize = [CCDirector sharedDirector].winSize;

//aux layer to make screenshot
CCLayerColor* blankLayer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 0) width:winSize.width height:winSize.height];

blankLayer.position = ccp(winSize.width/2, winSize.height/2);

CCRenderTexture* rtx = [CCRenderTexture renderTextureWithWidth:winSize.width height:winSize.height];

//this part is the same as yours
[rtx begin];
[blankLayer visit];
[[[CCDirector sharedDirector] runningScene] visit];
[rtx end];


//this part makes a rectangle from the screenshot with size.width and 200 height. Change it

UIImage *tempImage =[rtx getUIImage];
CGRect imageBoundary = CGRectMake(0, 0, winSize.width, 200);
UIGraphicsBeginImageContext(imageBoundary.size);
CGContextRef context = UIGraphicsGetCurrentContext();

// translated rectangle for drawing sub image
CGRect drawRect = CGRectMake(-imageBoundary.origin.x, -imageBoundary.origin.y, tempImage.size.width, tempImage.size.height);

// clip to the bounds of the image context
CGContextClipToRect(context, CGRectMake(0, 0, imageBoundary.size.width, imageBoundary.size.height));

// draw image
[tempImage drawInRect:drawRect];
UIImage* subImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


return subImage;

//and there you have an UIImage 


}

This code works well when making screenshots of a part of the screen. Maybe you can try deleting the code part where it makes a temp image with a CGRect. But I recommend you to change the values like this:

CGRect imageBoundary = CGRectMake(0, 0, winSize.width, winSize.height);

So it will make a full screenshot.

mursang
  • 586
  • 5
  • 24
0

You can use "Kamcord" framework, with this framework you can take screenshots as well as you can also record video of your game play and you can replay them..

Himanshu Garg
  • 599
  • 9
  • 21
0

I use it. This is for Cocos2d v3. Weak needs for memory returning. Because there're some problems with it. If you'll remove weak, you memory usage will increase every time you call this method. I think it's best way to do.

+(UIImage *)screenshot
{ 
    [CCDirector sharedDirector].view.alpha = 1.0;
    [CCDirector sharedDirector].nextDeltaTimeZero = YES;

    CGSize winSize = [[CCDirector sharedDirector] viewSize];
    CCRenderTexture* renTxture = [CCRenderTexture renderTextureWithWidth:winSize.width
                                                                  height:winSize.height
                                                             pixelFormat:CCTexturePixelFormat_RGB565];
    [renTxture begin];

    MainScene* tempScene = (MainScene *)[[CCDirector sharedDirector] runningScene];
    MainScene* __weak scene = tempScene;
    [scene visit];
    [renTxture end];

    UIImage *tempImage = [renTxture getUIImage];
    UIImage * __weak image = tempImage;

    return image;
}
Mr.Trent
  • 1
  • 1