iphone - OpenGL ES [CAEAGLLayer] 内容的高分辨率视网膜显示屏幕截图

标签 iphone objective-c ios xcode opengl-es

我正在为我的画图应用程序使用 GLPaint 示例,我需要截取 OpenGL ES [CAEAGLLayer] 渲染内容的屏幕截图。我正在使用函数:

-(UIImage *)snapUIImage
{

    int s = 2;


    const int w = self.frame.size.width;
    const int h = self.frame.size.height;
    const NSInteger myDataLength = w * h * 4 * s * s;


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


    glReadPixels(0, 0, w*s, h*s, 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 < h*s; y++)
    {
        memcpy( buffer2 + (h*s - 1 - y) * w * 4 * s, buffer + (y * 4 * w * s), w * 4 * s );
    }
    free(buffer); // work with the flipped buffer, so get rid of the original one.

    // make data provider with data.
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
    // prep the ingredients
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4 * w * s;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaLast;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

    // make the cgimage
    CGImageRef imageRef = CGImageCreate(w*s, h*s, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
    // then make the uiimage from that
    UIImage *myImage = [ UIImage imageWithCGImage:imageRef scale:s orientation:UIImageOrientationUp ];


    CGImageRelease( imageRef );

    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorSpaceRef);
    // buffer2 will be destroyed once myImage is autoreleased.

    return myImage;

}



-(void)captureToPhotoAlbum {
        UIImage *image = [self snapUIImage];
            UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
}

上面的代码正在运行我正在获取图像,但它不是高分辨率 [Retina 显示] 图像。请帮忙。

提前致谢。

最佳答案

工作代码解决方案

- (UIImage*) getGLScreenshot
{
    int myWidth  = self.frame.size.width*2;
    int myHeight = self.frame.size.height*2;
    int myY = 0;
    int myX = 0;
    int bufferLength = (myWidth*myHeight*4);
    
    //unsigned char buffer[bufferLength];
    unsigned char* buffer =(unsigned char*)malloc(bufferLength);
    glReadPixels(myX, myY, myWidth, myHeight, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    
    CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, buffer, bufferLength, NULL);
    CGImageRef iref = CGImageCreate(myWidth,myHeight,8,32,myWidth*4,CGColorSpaceCreateDeviceRGB(),
                                    kCGBitmapByteOrderDefault,ref,NULL, true, kCGRenderingIntentDefault);
    uint32_t* pixels = (uint32_t *)malloc(bufferLength);
    CGContextRef context = CGBitmapContextCreate(pixels, myWidth, myHeight, 8, myWidth*4, CGImageGetColorSpace(iref),
                                                 kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Big);
    CGContextTranslateCTM(context, 0.0, myHeight);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, CGRectMake(0.0, 0.0, myWidth, myHeight), iref);
    CGImageRef outputRef = CGBitmapContextCreateImage(context);
    
    UIImage *image = nil;
    if(regardOrientation) {
        UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
        if (deviceOrientation == UIDeviceOrientationPortraitUpsideDown) {
            image = [UIImage imageWithCGImage:outputRef scale:1 orientation:UIImageOrientationDown];
        } else if (deviceOrientation == UIDeviceOrientationLandscapeLeft) {
            image = [UIImage imageWithCGImage:outputRef scale:1 orientation:UIImageOrientationLeft];
        } else if (deviceOrientation == UIDeviceOrientationLandscapeRight) {
            image = [UIImage imageWithCGImage:outputRef scale:1 orientation:UIImageOrientationRight];
        } else {
            image = [UIImage imageWithCGImage:outputRef scale:1 orientation:UIImageOrientationUp];
        }
    } else {
        image = [UIImage imageWithCGImage:outputRef scale:1 orientation:UIImageOrientationUp];
    }
    
    image = [UIImage imageWithCGImage:outputRef scale:1 orientation:UIImageOrientationUp];
    
    CGImageRelease(iref);
    CGImageRelease(outputRef);
    CGContextRelease(context);
    CGDataProviderRelease(ref);
    free(buffer);
    free(pixels);
    
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    
    return image;
}

关于iphone - OpenGL ES [CAEAGLLayer] 内容的高分辨率视网膜显示屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6758909/

相关文章:

iphone - 带有徽章编号的 UISegmentedControl

objective-c - Objective-C : Code Understanding

objective-c - 寻找有关在iOS中访问NWS严重警报的建议

ios - SKSpriteNode 在随机路径上的平滑运动

ios - 使用导航 Controller 处理页面之间的数据

ios - 快速检查对象的属性是否存在

objective-c - 数据上传期间在 UITableView 上的 UIProgressView+Activity Indicator View

iphone - 如何使用包含彩色图像的按钮自定义 UIToolbar?

iphone - 应用程序进入前台时刷新值

iphone - 如何在 ios 中切换隐藏和查看密码?