2012-06-01 50 views
0

當試圖將opengl視圖快照作爲UIImage進行多采樣時,圖像顏色是不同的。OpenGL ES快照爲iOS中的多色採樣提供奇數顏色

當多重採樣關閉時,它是正確的。 這是我正在採取快照:

- (UIImage*)snapshot 
{ 

    GLint backingWidth, backingHeight; 

    backingWidth = framebufferWidth; 
    backingHeight = framebufferHeight; 

    NSInteger myDataLength = backingWidth * backingHeight * 4; 

    GLubyte *buffer = (GLubyte *) malloc(myDataLength); 
    glReadPixels(0, 0, backingWidth, backingHeight, GL_RGBA, GL_UNSIGNED_BYTE, buffer); 

    // gl renders "upside down" so swap top to bottom into new array. 
    GLubyte *buffer2 = (GLubyte *) malloc(myDataLength); 

    for(int y = 0; y < backingHeight; y++) { 
     for(int x = 0; x < backingWidth * 4; x++) { 
      buffer2[y*4*backingWidth + x] = buffer[(backingHeight - y -1) * backingWidth * 4 + x]; 
     } 
    } 
    // make data provider with data. 
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, myProviderReleaseData); 

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

    // make the cgimage 
    CGImageRef imageRef = CGImageCreate(backingWidth, backingHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent); 

    // then make the uiimage from that 
    UIImage *image1 = [UIImage imageWithCGImage:imageRef]; 

    CGImageRelease(imageRef); 
    CGColorSpaceRelease(colorSpaceRef); 
    CGDataProviderRelease(provider); 
    free(buffer); 

    return image1; 
} 

這裏是採取快照結果:

opengl view

snapshot of the same view

第一個是OpenGL的看法,我繪畫和第二張圖片是我獲得上述代碼的圖片的快照。

我沒有使用GLKit框架。想知道爲什麼多重採樣混淆了快照。

回答

1

檢查您是否調用[EAGLContext:presentRenderBuffer]以OpenGL的視圖的快照

+0

感謝之前的方法,它的工作! – darshansonde