Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to develop an IOS SDK for Unity/COCOS/SpritKit game app to record video.Main program is to synthetic video after screenshots.

Now I do the test with IOS project from unity game on IOS 7.0 device and try to use Opengl function glreadpiexls() to read pixel buffer, but always got the black image which troubled me for weeks.
So can anyone give me some suggestions for the screenshot on iOS for Unity/COCOS/SpritKit games, and this is my current code.

Objective-C
int w = 320;//size.width;//viewport[2];//
int h = 480;//size.height;//viewport[3];//
NSInteger myDataLength = w * h * 4;    

// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *) malloc(myDataLength);

    glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
GLubyte temp = 0;
int index1 = 0;
int index2 = 0;
for(int y = 0; y < h - 1 - y; y++) {  // 交换
    index1 = y * 4 * w;
    index2 = (h -1 - y) * w * 4;
    for(int x = 0; x <w * 4; x++) {
        temp = buffer[index1 + x];
        buffer[index1 + x] = buffer[index2 + x];
        buffer[index2 + x] = temp;
    }
}

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


// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * w;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
// make the cgimage
CGImageRef imageRef = CGImageCreate(w, h, bitsPerComponent, bitsPerPixel, bytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrderDefault, provider, NULL, NO, kCGRenderingIntentDefault);

// myImage is alway nil when debug
UIImage *myImage = [ UIImage imageWithCGImage:imageRef scale:s orientation:UIImageOrientationUp ];    
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpaceRef);


What I have tried:

I try some solutions like to set the context and set the EAGLView drawing property before glreadpiexls() but not work. And some suggest to make sure to execute glreadpiexls() before presentRenderbuffer, but how . I can not modify the code from Unity.

Solutions like IOSuface depreciated on iOS 9.0 and seems only for UIView elements and IOSurface was depreciated on ios 9.0 and seems only for UIView elements. RenderTexture or Application.CaptureScreenshot are only for unity project, not for my IOS SDK.
Posted
Updated 5-Jun-16 15:48pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900