2012-03-24 57 views
3

我知道如何使用CCRenderTexture在CoCos2d中保存屏幕的例子不勝枚舉,但它們似乎不適用於我。我爲客戶編寫了一本着色書應用程序,他們當然希望能夠保存圖像。我嘗試了很多不同的方式,並混淆了一堆示例,但無濟於事。最近,我一直在收到此錯誤:使用CCRenderTexture在CoCos2d V2.xx中保存屏幕截圖

2012-03-24 13:07:03.749 Coloring Book[823:1be03] cocos2d: ERROR: Failed to save file:/Users/macbookpro/Library/Application Support/iPhone Simulator/5.1/Applications/76F88977-AD3A-47B8-8026-C9324BB3636E/Documents/Users/macbookpro/Library/Application Support/iPhone Simulator/5.1/Applications/76F88977-AD3A-47B8-8026-C9324BB3636E/Documents/testimagename.png to disk

我從設備運行時得到類似的東西。這裏是我的屏幕截圖代碼:

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

這可能是簡單的,但它已經讓我堅持了幾天!請堆棧溢出,讓我感到愚蠢,並解決我的問題!

+0

步入saveToFile方法。如果它返回一個NSError對象,看看是否給你更多的信息(即拒絕權限,沒有足夠的磁盤空間等)。如果這沒有幫助將任何文件保存到相同的路徑,例如使用NSString writeToFile並提供一個NSError對象,可能會給你更多的信息。 – LearnCocos2D 2012-03-24 19:43:05

+0

@ LearnCocos2D - 首先,感謝您的回覆!好吧,我走過了,它沒有給出任何錯誤。它通過'if(format == kCCImageFormatPNG) \t \t imageData = UIImagePNGRepresentation(image);'saveToFile節的段。它只是返回FALSE成功變量,所以它必須在提交失敗。我將盡快保存其他內容。 – 2012-03-24 20:13:06

+0

@ LearnCocos2D好的,我只是用writeToFile將文件保存到Documents文件夾中,所以不應該是路徑。 – 2012-03-24 20:48:29

回答

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); 
}