Take screenshot on a macOS app

Hi there,
I’m using xCode11 with cocos2d 3.17. I have a iOS app to porting as a macOS app. I need help for the method take screenshot. this is the actual method on iOS app:

   void BridgeIOS::takeScreenshot(unsigned char * img, ssize_t size, int width, int height) {
  
  CGColorSpaceRef colorSpace  = CGColorSpaceCreateDeviceRGB();
  CGContextRef ctx = CGBitmapContextCreate(img,
                                           width,
                                           height,
                                           8,
                                           width * 4,
                                           colorSpace,
                                           kCGImageAlphaPremultipliedLast );
  
  CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
  UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
  CGImageRelease(imageRef);
  
  UIImageWriteToSavedPhotosAlbum(rawImage, nil, nil, nil);
  
  CGColorSpaceRelease(colorSpace);
  CGContextRelease(ctx);
}

For the porting I changed it like this:

void BridgeMac::takeScreenshot(unsigned char * img, ssize_t size, int width, int height) {
  
  CGColorSpaceRef colorSpace  = CGColorSpaceCreateDeviceRGB();
  CGContextRef ctx = CGBitmapContextCreate(img,
                                           width,
                                           height,
                                           8,
                                           width * 4,
                                           colorSpace,
                                           kCGImageAlphaPremultipliedLast );
  
  CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
  CIImage* rawImage = [CIImage imageWithCGImage:imageRef];
  CGImageRelease(imageRef);
  
  UIImageWriteToSavedPhotosAlbum(rawImage, nil, nil, nil);     //error in this line
  
  CGColorSpaceRelease(colorSpace);
  CGContextRelease(ctx);
}

There is a corresponding method for UIImageWriteToSavedPhotosAlbum but for iOS application? thank you if someone can answer me

Are you looking for Objective-C solution or C++?

If you want to do this from Cocos2d-x, I have used RenderTexture to take screenshots. There are examples of RenderTexture in cpp-tests.

Sorry, to get the screenshot I use render texture (c++) as you said! for save it on the Mac, how can I do it? (using Objective-C)

Why can’t you use RenderTexture on macOS?

Are you looking for something like this? I use this for macOS app

       FileUtils::getInstance()->createDirectory(FileUtils::getInstance()->getWritablePath() + "screenshots");
            
            char ch[128];
            sprintf(ch, "/%s/screenshots/CaptureScreen%lld.png",FileUtils::getInstance()->getWritablePath().c_str() , utils::getTimeInMilliseconds());
            utils::captureScreen([=](bool succeed, const std::string& outputFile){
                if (succeed)
                   {
                       CCLOG("Capture screen success.");
                       
                       
                   }
                   else
                   {
                       CCLOG("Capture screen failed.");
                   }
            },ch);

Why can’t you use RenderTexture on macOS?

@slackmoehrle I’m so sorry, I haven’t been clear. I use render texture on iOS like this.

  RenderTexture *tex = RenderTexture::create(int(screenSize.width), int(screenSize.height), Texture2D::PixelFormat::RGBA8888);
  tex->setPosition(Vec2(screenSize.width/2, screenSize.height/2));
  tex->begin();
  background->visit();
  paintNode->visit();
  tex->end();
  
  Director::getInstance()->getRenderer()->render();

My problem it’s save it on the Mac photo library.

Are you looking for something like this? I use this for macOS app

Yes! It’s what I was looking for! thank you very much! @bilalmirza

1 Like

oh, yes use @bilalmirza solution to capture the whole screen and save it to the photo library.

2 Likes

@slackmoehrle thank you so much for replying, in any case

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.