将Paint Drawing中的图像保存到iPad视网膜中

时间:2013-12-10 06:46:20

标签: ios ipad opengl-es drawing

我正在尝试将绘图保存为来自Apple示例代码的GLPaint应用中的图像 保存图像在iPad(非视网膜)中工作正常,但它在iPad(视网膜)上运行时发出 每当我在iPad视网膜上运行我的应用程序时,图像大小是原始的1/4 任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

如果您使用的是glReadPixels,则您似乎插入了错误的参数作为宽度和高度。您需要获取GL缓冲区的宽度和高度,并将它们插入glReadPixels而不是帧宽和高度。对于视网膜而言,这将是帧尺寸长度的两倍。

从提到的示例中可以看出backingWidthbackingHeight,您应该可以随时使用它们获取它们:

glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);

因此请使用glReadPixels(0, 0, backingWidth, backingHeight, ...)并确保您读取的缓冲区也足够大。对于RGBA,它将是backingWidth*backingHeight*4个字节。

答案 1 :(得分:0)

我见过其他方法泄漏内存,这里是一个剪切我发现并修改以从GLPaint中捕获图像。

-(BOOL)iPhoneRetina{
    return ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale == 2.0))?YES:NO;
}

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

-(UIImage *) glToUIImage{

    int imageWidth, imageHeight;

    int scale = [self iPhoneRetina]?2:1;

    imageWidth = self.frame.size.width*scale;
    imageHeight = self.frame.size.height*scale;

    NSInteger myDataLength = imageWidth * imageHeight * 4;

    // allocate array and read pixels into it.
    GLubyte *buffer = (GLubyte *) malloc(myDataLength);
    glReadPixels(0, 0, imageWidth, imageHeight, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

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

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

    // make the cgimage

    CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

    UIImage *myImage = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationDownMirrored]; //Render image flipped, since OpenGL's data is mirrored

    CGImageRelease(imageRef);
    CGColorSpaceRelease(colorSpaceRef);

    CGDataProviderRelease(provider);

    return myImage;
}