2012-03-24 41 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

+0

@ LearnCocos2D - まず、返信いただきありがとうございます! OK、私は一歩踏み込んだが、何の誤りもない。 saveToFileセクションの 'if(format == kCCImageFormatPNG) \t \t imageData = UIImagePNGRepresentation(image);'セグメントを通過します。成功変数に対してはFALSEを返すだけなので、コミット中に失敗する必要があります。他の何かを素早く保存しようとしています。 –

+0

@ LearnCocos2D OK、私はちょうどwriteToFileでドキュメントフォルダにファイルを保存したので、パスではありません。 –

答えて

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); 
} 
関連する問題