Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8417941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:10:56+00:00 2026-06-10T02:10:56+00:00

I have a working screenshot code that I got from here (stackoverflow). Problem is

  • 0

I have a working screenshot code that I got from here (stackoverflow). Problem is I can’t do it asynchronously. My screen is all black.

Is this possible? If yes, how will I do it?

Here’s the code I’m using

CGSize winSize = [CCDirector sharedDirector].winSize;
int screenScale = [UIScreen mainScreen].scale;
int width = winSize.width * screenScale;
int height = winSize.height * screenScale;

NSInteger myDataLength = width * height * 4;

// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

// gl renders "upside down" so swap top to bottom into new array.
// there's gotta be a better way, but this works.
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
for(int y = 0; y < height; y++)
{
    for(int x = 0; x < width * 4; x++)
    {
        buffer2[(height-(1*screenScale) - y) * width * 4 + x] = buffer[y * 4 * width + x];
    }
}

// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);

// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

// make the cgimage
CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

// then make the uiimage from that
UIImage *myImage = [UIImage imageWithCGImage:imageRef];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:10:57+00:00Added an answer on June 10, 2026 at 2:10 am

    The following is a suggestion you could try to do to do SOME stuff on the separate thread, but you still have to get the OpenGL pixels from the main thread.
    My suggestion would be to save the pixels to the buffer on the main thread, and then schedule the background task to create the UIImage with that buffer.

    - (void) takeScreenshot 
    {
    CGSize winSize = [CCDirector sharedDirector].winSize;
    int screenScale = [UIScreen mainScreen].scale;
    int width = winSize.width * screenScale;
    int height = winSize.height * screenScale;
    
    NSInteger myDataLength = width * height * 4;
    
    // allocate array and read pixels into it.
    buffer = (GLubyte *) malloc(myDataLength); // NOW PART OF THE CLASS DEFINITION
    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    [self performSelectorInBackground:@selector(finishScreenshot) withObject:nil];
    }
    

    Instead of saving the buffer array as a class variable, you could actually send the buffer as NSData on the performSelectorInBackground call, and then add the parameter on finishScreenshot.

    -(void)finishScreenshot{
    
    CGSize winSize = [CCDirector sharedDirector].winSize;
    int screenScale = [UIScreen mainScreen].scale;
    int width = winSize.width * screenScale;
    int height = winSize.height * screenScale;
    // gl renders "upside down" so swap top to bottom into new array.
    // there's gotta be a better way, but this works.
    GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
    for(int y = 0; y < height; y++)
    {
        for(int x = 0; x < width * 4; x++)
        {
            buffer2[(height-(1*screenScale) - y) * width * 4 + x] = buffer[y * 4 * width + x];
        }
    }
    
    // make data provider with data.
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
    
    // prep the ingredients
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4 * width;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    
    // make the cgimage
    CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
    
    // then make the uiimage from that
    UIImage *myImage = [UIImage imageWithCGImage:imageRef];
    
    }
    

    As I said, I’m not sure of the viability of this, but this should work. Try to do this and check if it really is worth the effort performance wise.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on this all day, much of my code is from
I have working registration script the only problem is that i do not know
I have a little problem. I am working in a Title Screen or main
I'm working on an app that can retrieve images from the web. Rather than
I have a JSON feed that I'm parsing. Here's the following code samples: JSON
I have working code of js, it shows different div for selected option. <html>
I am working on a program that includes a selection of code which is
I have the javascript validation working good for my own code. I want to
I'm adding mouse rotation to my 2D drawing program. I've got all the code
I have a very large view(~8000x8000) that I'd like to take a screenshot of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.