diff --git a/cocos/2d/CCRenderTexture.cpp b/cocos/2d/CCRenderTexture.cpp index f8d08d3185..7738341537 100644 --- a/cocos/2d/CCRenderTexture.cpp +++ b/cocos/2d/CCRenderTexture.cpp @@ -409,29 +409,47 @@ void RenderTexture::visit(Renderer *renderer, const Mat4 &parentTransform, uint3 _orderOfArrival = 0; } -bool RenderTexture::saveToFile(const std::string& filename) +bool RenderTexture::saveToFile(const std::string& filename, bool isRGBA) { - return saveToFile(filename,Image::Format::JPG); + std::string basename(filename); + std::transform(basename.begin(), basename.end(), basename.begin(), ::tolower); + + if (basename.find(".png") != std::string::npos) + { + return saveToFile(filename, Image::Format::PNG, isRGBA); + } + else if (basename.find(".jpg") != std::string::npos) + { + if (isRGBA) CCLOG("RGBA is not supported for JPG format."); + return saveToFile(filename, Image::Format::JPG, false); + } + else + { + CCLOG("Only PNG and JPG format are supported now!"); + } + + return saveToFile(filename, Image::Format::JPG, false); } -bool RenderTexture::saveToFile(const std::string& fileName, Image::Format format) +bool RenderTexture::saveToFile(const std::string& fileName, Image::Format format, bool isRGBA) { CCASSERT(format == Image::Format::JPG || format == Image::Format::PNG, "the image can only be saved as JPG or PNG format"); + if (isRGBA && format == Image::Format::JPG) CCLOG("RGBA is not supported for JPG format"); std::string fullpath = FileUtils::getInstance()->getWritablePath() + fileName; _saveToFileCommand.init(_globalZOrder); - _saveToFileCommand.func = CC_CALLBACK_0(RenderTexture::onSaveToFile,this,fullpath); + _saveToFileCommand.func = CC_CALLBACK_0(RenderTexture::onSaveToFile, this, fullpath, isRGBA); Director::getInstance()->getRenderer()->addCommand(&_saveToFileCommand); return true; } -void RenderTexture::onSaveToFile(const std::string& filename) +void RenderTexture::onSaveToFile(const std::string& filename, bool isRGBA) { Image *image = newImage(true); if (image) { - image->saveToFile(filename.c_str(), true); + image->saveToFile(filename.c_str(), !isRGBA); } CC_SAFE_DELETE(image); diff --git a/cocos/2d/CCRenderTexture.h b/cocos/2d/CCRenderTexture.h index 09ba451df7..187acc18b3 100644 --- a/cocos/2d/CCRenderTexture.h +++ b/cocos/2d/CCRenderTexture.h @@ -103,12 +103,12 @@ public: /** saves the texture into a file using JPEG format. The file will be saved in the Documents folder. Returns true if the operation is successful. */ - bool saveToFile(const std::string& filename); + bool saveToFile(const std::string& filename, bool isRGBA = true); /** saves the texture into a file. The format could be JPG or PNG. The file will be saved in the Documents folder. Returns true if the operation is successful. */ - bool saveToFile(const std::string& filename, Image::Format format); + bool saveToFile(const std::string& filename, Image::Format format, bool isRGBA = true); /** Listen "come to background" message, and save render texture. It only has effect on Android. @@ -222,7 +222,7 @@ protected: void onClear(); void onClearDepth(); - void onSaveToFile(const std::string& fileName); + void onSaveToFile(const std::string& fileName, bool isRGBA = true); Mat4 _oldTransMatrix, _oldProjMatrix; Mat4 _transformMatrix, _projectionMatrix; diff --git a/cocos/platform/ios/CCImage.mm b/cocos/platform/ios/CCImage.mm index 71ebce91a2..ab288f8f65 100644 --- a/cocos/platform/ios/CCImage.mm +++ b/cocos/platform/ios/CCImage.mm @@ -42,7 +42,9 @@ bool cocos2d::Image::saveToFile(const std::string& filename, bool isToRGB) bool saveToPNG = false; bool needToCopyPixels = false; - if (std::string::npos != filename.find(".png")) + std::string basename(filename); + std::transform(basename.begin(), basename.end(), basename.begin(), ::tolower); + if (std::string::npos != basename.find(".png")) { saveToPNG = true; }