1

我有一个从这里(stackoverflow)获得的工作截图代码。问题是我不能异步地做到这一点。我的屏幕全黑。

这可能吗?如果是,我将如何做?

这是我正在使用的代码

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];
4

2 回答 2

3

以下是您可以尝试在单独的线程上做一些事情的建议,但您仍然必须从主线程获取 OpenGL 像素。我的建议是将像素保存到主线程上的缓冲区,然后安排后台任务以使用该缓冲区创建 UIImage。

- (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];
}

除了将缓冲区数组保存为类变量,您实际上可以在 performSelectorInBackground 调用中将缓冲区作为 NSData 发送,然后在 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];

}

正如我所说,我不确定这是否可行,但这应该可行。尝试这样做并检查它是否真的值得付出努力。

于 2012-08-18T15:52:54.733 回答
2

It's not possible.

The OpenGL view can only be modified and accessed from the main thread, so any attempt to grab the OpenGL framebuffer's contents in another thread will fail.

于 2012-08-18T10:58:18.667 回答