2013-12-10 50 views
0

我試圖從蘋果示例代碼中保存圖形作爲從GLPaint應用程序的圖像。
保存圖像在iPad(非視網膜)中工作正常,但它在iPad(視網膜)上運行時發出。
每當我在iPad視網膜上運行我的應用程序時,圖像大小是原來的1/4。
任何人都可以幫我解決這個問題嗎?保存圖像從GLPaint繪圖到iPad鼠標

回答

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字節。

+0

Thnx @ Matic..it works fine .. – Jhanvi

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; 
}