【发布时间】:2012-03-24 17:14:36
【问题描述】:
我知道有很多关于如何使用 CCRenderTexture 在 CoCos2d 中保存屏幕的示例,但它们似乎对我不起作用。我为客户编写了一个着色书应用程序,他们当然希望能够保存图像。我尝试了很多不同的方法,并且对一堆例子进行了混蛋无济于事。最近,我一直收到这个错误:
2012-03-24 13:07:03.749 图画书[823:1be03] cocos2d:错误: 保存文件失败:/Users/macbookpro/Library/Application 支持/iPhone 模拟器/5.1/Applications/76F88977-AD3A-47B8-8026-C9324BB3636E/Documents/Users/macbookpro/Library/Application Support/iPhone 模拟器/5.1/Applications/76F88977-AD3A-47B8-8026-C9324BB3636E/Documents/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,让我感到愚蠢并解决我的问题!
【问题讨论】:
-
进入 saveToFile 方法。如果它返回一个 NSError 对象,请查看它是否为您提供了更多信息(即权限被拒绝、磁盘空间不足等)。如果这无济于事,请尝试将任何文件保存到同一路径,例如使用 NSString writeToFile 并提供一个 NSError 对象,该对象可能会为您提供更多信息。
-
@LearnCocos2D - 首先,感谢您的回复!好的,我通过了,它没有给出任何错误。它通过 saveToFile 部分的
if( format == kCCImageFormatPNG ) imageData = UIImagePNGRepresentation( image );段。它只是为成功变量返回 FALSE,因此它必须在提交期间失败。我将尝试快速保存其他内容。 -
@LearnCocos2D 好的,我刚刚使用 writeToFile 将文件保存到 Documents 文件夹中,所以不应该是路径。
标签: ios cocos2d-iphone screenshot