1

我打算将 aUIView绘制OpenGL ES到a 中的快照UIImage,这是我使用的代码:

 if(context){
    [EAGLContext setCurrentContext:context];
    glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
    NSInteger dataLength = framebufferWidth * framebufferHeight * 4;

    GLubyte* data = (GLubyte*)malloc(dataLength) ; // malloc(myDataLength);
    glReadPixels(0, 0, framebufferWidth, framebufferHeight, GL_RGBA, GL_UNSIGNED_BYTE, data);

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

    // prep the ingredients
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4 * framebufferWidth;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

    // make the cgimage
    CGImageRef result = CGImageCreate(framebufferWidth, framebufferHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

    // then make the uiimage from that
    UIImage *image = [UIImage imageWithCGImage:result];
    CGImageRelease(result);
    CGColorSpaceRelease(colorSpaceRef);
    CGDataProviderRelease(provider);
    //free(data);

    return image;
}

我本来打算使用free(data);before return,但这会引发BAD_ACCESS运行时错误。

所以我的问题是,如何free正确?或者我应该free在图像发布后他们?

4

2 回答 2

1

您应该CGDataProviderCreateWithData's CGDataProviderReleaseDataCallback按照此处所述使用。

只需添加 freeData 函数回调:

void freeData(void *info, const void *data, size_t size) 
{
    free((void*)data);
}

并将其传递给CGDataProviderCreateWithData

CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data, dataLength, freeData);
于 2013-02-14T12:56:45.890 回答
0

这是我当前的代码,它工作得很好。

if(context){ 
    [EAGLContext setCurrentContext:context];
    glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
    NSInteger dataLength = width * height * 4;
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4 * width;
    GLubyte* data = (GLubyte*)malloc(dataLength) ;
    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);

    // make upside down
    GLubyte* swp = (GLubyte*)malloc(width * 4);
    for (int h = 0; h < height / 2; h++) {
        memcpy(swp, data + (height - 1 - h) * bytesPerRow, bytesPerRow);
        memcpy(data + (height - 1 - h) * bytesPerRow, data + h * bytesPerRow, bytesPerRow);
        memcpy(data + h * bytesPerRow, swp, bytesPerRow);
    }

    NSData* nsData = [NSData dataWithBytesNoCopy:data length:dataLength freeWhenDone:YES];
    // make data provider with data.
    CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)nsData);

    // prep the ingredients
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

    // make the cgimage
    CGImageRef result = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
    CGColorSpaceRelease(colorSpaceRef);
    CGDataProviderRelease(provider);

    // then make the uiimage from that
    UIImage *image = [UIImage imageWithCGImage:result];
    CGImageRelease(result);

    return image;
 }
于 2013-02-21T08:25:32.100 回答