gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:12:44 25 4
gpt4 key购买 nike

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

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

这可能是一件简单的事情,但几天来一直让我抓狂!请 Stack Overflow 让我感到愚蠢并解决我的问题!

最佳答案

我遇到的问题归结为定义路径。你不需要定义设备的 Documents 部分的路径,Cocos2D 默认将其保存到 Documents 中。我将它们放在一起(请注意,非常感谢 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);
}

关于ios - 使用 CCRenderTexture 在 CoCos2d V2.xx 中保存屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9853750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com