有一种有效的方法吗?目前我使用
[renderTexture begin];
[self.view visit];
[renderTexture end];
但应用因此而紧张不安。
我读过我可以为此复制帧缓冲区的内容。真的吗?有人可以指导我如何做到这一点。
答案 0 :(得分:0)
我使用了一种适合我的方法。它做了一个截图,然后选择它的一个框架。您可以更改它,因此它会从整个屏幕截取屏幕截图。 这是代码。
-(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
}
此代码在制作部分屏幕截图时效果很好。也许您可以尝试删除使用CGRect创建临时图像的代码部分。但我建议你改变这样的值:
CGRect imageBoundary = CGRectMake(0, 0, winSize.width, winSize.height);
所以它会制作完整的截图。
答案 1 :(得分:0)
您可以使用“Kamcord”框架,使用此框架可以截取屏幕截图,还可以录制游戏视频,然后重播它们。
答案 2 :(得分:0)
我用它。这是针对Cocos2d v3的。内存返回的需求微弱。因为它有一些问题。如果您将删除弱,则每次调用此方法时内存使用量都会增加。我认为这是最好的方法。
+(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;
}