1

我需要在我的cocos2d应用程序中截取屏幕截图。即使在堆栈溢出中,我也进行了很多搜索。然后我找到了以下代码:

+(UIImage*) screenshotWithStartNode:(CCNode*)stNode
{
    [CCDirector sharedDirector].nextDeltaTimeZero = YES;

    CGSize winSize = [[CCDirector sharedDirector] winSize];
    CCRenderTexture* renTxture = 
    [CCRenderTexture renderTextureWithWidth:winSize.width 
                                 height:winSize.height];
    [renTxture begin];
    [stNode visit];
    [renTxture end];

    return [renTxture getUIImage];
}

现在,

问题:上面的代码给了我整个截图。但我需要自定义的屏幕截图,例如,在CGRect(50,50,100,200).

谁能帮帮我吗..?谢谢...

4

1 回答 1

2

哇...找到了我需要的东西。我改变了上述方法,如:

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

    UIImage *tempImage = [rtx getUIImage];
    CGRect imageBoundary = CGRectMake(100, 100, 300, 300);

    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];

    // grab image
    UIImage* subImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return subImage;

}
于 2013-11-02T18:29:52.763 回答