diff --git a/cocos/base/ccUtils.cpp b/cocos/base/ccUtils.cpp index 8977e345be..b5dccb034f 100644 --- a/cocos/base/ccUtils.cpp +++ b/cocos/base/ccUtils.cpp @@ -163,17 +163,22 @@ void captureNode(Node* startNode, std::function)> imageCallba } // [DEPRECATED] -void captureScreen(const std::function& afterCaptured, const std::string& filename) +void captureScreen(std::function afterCap, const std::string& filename) { std::string outfile; if (FileUtils::getInstance()->isAbsolutePath(filename)) outfile = filename; else outfile = FileUtils::getInstance()->getWritablePath() + filename; - captureScreen([=,fullPath=std::move(outfile)](RefPtr image) { - bool ok = image && image->saveToFile(fullPath, false); - - afterCaptured(ok, fullPath); + + captureScreen([_afterCap = std::move(afterCap), _outfile = std::move(outfile)](RefPtr image) mutable { + AsyncTaskPool::getInstance()->enqueue(AsyncTaskPool::TaskType::TASK_IO, [_afterCap = std::move(_afterCap), image = std::move(image), _outfile = std::move(_outfile)]() mutable + { + bool ok = image->saveToFile(_outfile); + Director::getInstance()->getScheduler()->performFunctionInCocosThread([ok, _afterCap = std::move(_afterCap), _outfile = std::move(_outfile)]{ + _afterCap(ok, _outfile); + }); + }); }); } diff --git a/cocos/base/ccUtils.h b/cocos/base/ccUtils.h index 11097a5442..bfe25b80de 100644 --- a/cocos/base/ccUtils.h +++ b/cocos/base/ccUtils.h @@ -91,7 +91,7 @@ namespace utils * base filename ("hello.png" etc.), don't use a relative path containing directory names.("mydir/hello.png" etc.). * @since v4.0 */ - CC_DLL void captureScreen(const std::function& afterCaptured, const std::string& filename); + CC_DLL void captureScreen(std::function afterCap, const std::string& filename); /** Find children by name, it will return all child that has the same name. * It supports c++ 11 regular expression. It is a helper function of `Node::enumerateChildren()`.