2013-11-02 13 views
1

我需要在我的cocos2d應用程序中截取屏幕截圖。我搜索了很多,即使在堆棧溢出。然後我發現下面的代碼:如何在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)之內。

任何人都可以請幫助我..?謝謝...

回答

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; 

} 
相關問題