使用CCRenderTexture在CoCos2d V2.xx中保存屏幕截图

时间:2012-03-24 17:14:36

标签: ios cocos2d-iphone screenshot

我知道有很多关于如何使用CCRenderTexture在CoCos2d中保存屏幕的例子,但它们似乎对我不起作用。我为客户编写了一本着色书应用程序,他们当然希望能够保存图像。我已经尝试了很多不同的方法,并将一堆例子混为一谈无济于事。最近,我一直收到这个错误:

  

2012-03-24 13:07:03.749着色书[823:1be03] cocos2d:错误:   保存文件失败:/ Users / macbookpro / Library / Application   支持/ iPhone   模拟器/ 5.1 /应用程序/ 76F88977-AD3A-47B8-8026-C9324BB3636E / Documents / Users / macbookpro / Library / Application Support / iPhone   模拟器/ 5.1 /应用/ 76F88977-AD3A-47B8-8026-C9324BB3636E /文档/ testimagename.png   到磁盘

从设备运行时我得到类似的东西。这是我的截图代码:

- (void) takeScreenShot
  {
     NSString* file = @"testimagename.png";

NSArray* paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* screenshotPath = [documentsDirectory 
                            stringByAppendingPathComponent:file];

[CCDirector sharedDirector].nextDeltaTimeZero = YES;

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

// save as file as PNG
[rtx  saveToFile:screenshotPath
         format:kCCImageFormatPNG];
 }

这可能很简单,但它已经让我疯了几天!请Stack Overflow让我感到愚蠢并解决我的问题!

1 个答案:

答案 0 :(得分:6)

我遇到的问题归结为定义路径。您无需定义设备的“文档”部分的路径,Cocos2D默认将其保存到“文档”。我把它放在一起(非常感谢LearnCocos2D我正在使用的一些代码)来保存我想要的图层,然后将屏幕保存到图片库。

- (void) takeScreenShot
{

//name the file we want to save in documents
NSString* file = @"//imageforphotolib.png";

//get the path to the Documents directory
NSArray* paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* screenshotPath = [documentsDirectory 
                            stringByAppendingPathComponent:file];

[CCDirector sharedDirector].nextDeltaTimeZero = YES;

//creating standard screensize variable
CGSize winSize = [CCDirector sharedDirector].winSize;

//we're using transparancies as the images, 
//so we load this white page to give a backdrop
CCSprite *whitePage = [CCSprite spriteWithFile:@"whitePage.png"];
whitePage.position = ccp(winSize.width/2, winSize.height/2);

//create a render texture to hold our images
CCRenderTexture* rtx = 
[CCRenderTexture renderTextureWithWidth:winSize.width 
                                 height:winSize.height];
[rtx begin];// open the texture
[whitePage visit];//add a white page to the background
[Page visit];//put in the background image
[target visit];//put in the coloring layer
[rtx end];//close the texture

// save as file as PNG
[rtx  saveToFile:@"imageforphotolib.png"
         format:kCCImageFormatPNG];

//get the screenshot as raw data
NSData *data = [NSData dataWithContentsOfFile:screenshotPath];
//create an image from the raw data
UIImage *img = [UIImage imageWithData:data];
//save the image to the users Photo Library
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
 }