0

此函数返回黑色图像。我使用麻雀图书馆。

这是我的代码:

-(UIImage *) saveRectangle:(SPRectangle*)rectangle
{
    int numberOfComponents = 8;
    int width = rectangle.width;
    int height = rectangle.height;

    NSInteger bufferLenght = width * height * numberOfComponents;


    NSMutableData * buffer= [NSMutableData dataWithLength:bufferLenght];

    glPixelStorei(GL_PACK_ALIGNMENT, 4); // force 4 byte alignment

    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, [buffer mutableBytes]);


    CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, [buffer mutableBytes], bufferLenght, NULL);
    CGImageRef iref = CGImageCreate(width, height, 8, 32, width * 4, CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrderDefault, ref, NULL, true, kCGRenderingIntentDefault);
    uint32_t* pixels = (uint32_t *)malloc(bufferLenght);
    CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width*4, CGImageGetColorSpace(iref), kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Big);
    CGContextTranslateCTM(context, 0.0, height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, CGRectMake(0.0, 0.0, width, height), iref);
    CGImageRef outputRef = CGBitmapContextCreateImage(context);
    free(pixels);

    return [UIImage imageWithCGImage:outputRef];
}
4

1 回答 1

1

您显示的代码几乎没有 Sparrow 代码(除了非常类似于 CGRect 的 SPRectangle )。听起来您正在尝试保存 Sparrow 场景的矩形区域的捕获。Sparrow 打算将您从 OpenGL 实现细节中抽象出来。您确定仅使用 SPRenderTexture 不会更容易吗?http://doc.sparrow-framework.org/core/Classes/SPRenderTexture.html

还要确保您要返回 UIImage 而不是 SPImage。希望能帮助到你。

于 2013-01-20T16:51:22.240 回答