Missing part of the image when taking screenshot w

2019-08-06 05:07发布

问题:

I'm currently working on enabling support for retina display for my game. In the game, we have a feature that the user can take screenshot. We are using these part of code we found online a while ago and it's working fine when we are not supporting retina display:

CCDirector* director = [CCDirector sharedDirector];
CGSize size = [director winSizeInPixels];

//Create buffer for pixels
GLuint bufferLength = size.width * size.height * 4;
GLubyte* buffer = (GLubyte*)malloc(bufferLength);

//Read Pixels from OpenGL
glReadPixels(0, 100, size.width, size.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
//Make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, bufferLength, NULL);

//Configure image
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * size.width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef iref = CGImageCreate(size.width, size.height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

uint32_t* pixels = (uint32_t*)malloc(bufferLength);
CGContextRef context = CGBitmapContextCreate(pixels, size.width, size.height, 8, size.width * 4, CGImageGetColorSpace(iref), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

CGContextTranslateCTM(context, 0, size.height);
CGContextScaleCTM(context, 1.0f, -1.0f);

switch (director.deviceOrientation)
{
    case CCDeviceOrientationPortrait:
        break;
    case CCDeviceOrientationPortraitUpsideDown:
        CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(180));
        CGContextTranslateCTM(context, -size.width, -size.height);
        break;
    case CCDeviceOrientationLandscapeLeft:
        CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(-90));
        CGContextTranslateCTM(context, -size.width, 0);
        break;
    case CCDeviceOrientationLandscapeRight:
        CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(90));
        CGContextTranslateCTM(context, size.width * 0.5f, -size.height);
        break;
}

CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), iref);
UIImage *outputImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];

//Dealloc
CGDataProviderRelease(provider);
CGImageRelease(iref);
CGContextRelease(context);
free(buffer);
free(pixels);

return outputImage;

But when we enabled retina display in cocos 0.99.5. This functionality is a little messed up since it will miss a little left part of the image while the high is still correct. So I'm wondering if there is anything wrong with the code or am I doing anything wrong here?

Thank you in advance for any reply!