<1>JNSaveScreenUtils.h
// // JNSaveScreenUtils.h // JNTest // // Created by jianan on 15/6/3. // // #ifndef __JNTest__JNSaveScreenUtils__ #define __JNTest__JNSaveScreenUtils__ #include "cocos2d.h" USING_NS_CC; #include<string> using namespace std; #define winSize Director::getInstance()->getWinSize() class JNSaveScreenUtils : public Ref { public: JNSaveScreenUtils(); ~JNSaveScreenUtils(); static JNSaveScreenUtils* getInstance(); void captureScreen(string fileName); private: CC_SYNTHESIZE(string,_saveFilePath,SaveFilePath); }; #endif /* defined(__JNTest__JNSaveScreenUtils__) */<2>JNSaveScreenUtils.cpp
// // JNSaveScreenUtils.cpp // JNTest // // Created by jianan on 15/6/3. // // #include "JNSaveScreenUtils.h" using namespace utils; static JNSaveScreenUtils* instance = NULL; JNSaveScreenUtils::JNSaveScreenUtils(){ } JNSaveScreenUtils::~JNSaveScreenUtils(){ } JNSaveScreenUtils* JNSaveScreenUtils::getInstance(){ if(!instance){ instance = new JNSaveScreenUtils(); } return instance; } void JNSaveScreenUtils::captureScreen(string fileName){ //void captureScreen(const std::function<void(bool,const std::string&)>& afterCaptured,const std::string& filename); ::captureScreen([](bool b,string name){ log("the pic is saved: %s,file name:%s",b?"success":"field",name.c_str()); },fileName); _saveFilePath = FileUtils::getInstance()->getWritablePath() + fileName; }<3>
bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } this->setTag(999); Sprite* sp = Sprite::create("HelloWorld.png"); sp->setPosition(Vec2(winSize.width/2,winSize.height/2)); this->addChild(sp); scheduleOnce(schedule_selector(HelloWorld::Main),1.0f); return true; } void HelloWorld::Main(float dt){ string filePath = JNSaveScreenUtils::getInstance()->getSaveFilePath(); //利用截屏图片来创造新的精灵 Sprite* sp = Sprite::create(filePath); sp->setPosition(winSize.width/3,winSize.height/3); addChild(sp); }原文链接:https://www.f2er.com/cocos2dx/342789.html