代码之家  ›  专栏  ›  技术社区  ›  shannoga

在xcode中替换openGl绘图中的特定颜色?

  •  2
  • shannoga  · 技术社区  · 16 年前

    我现在工作的能力,用户保存到相机辊图纸。

    我有一个视图,其中包含用户用来绘制的图像,因此它必须可见,但不受绘图画布的影响。

    在上面我有一个用于绘图的视图。因为我之前说的,我把背景设置成黑色透明的-

    问题-

    一切都很好,直到我保存图像。显然,背景色是透明的黑色,所以图像有黑色的背景,而我需要它是白色的。

    有人能告诉我吗?

    沙尼


    嗨,大卫,谢谢你的回复。在我第一次实际使用它的时候。我红色多一点,并添加了代码的其余部分(我希望如此)这一次的背景变成白色,但绘制的线消失了。我试着检查每个点的缓冲区颜色,并在每次为0时将其替换为255。效果很好,背景变成了白色,线条保留了下来,但它们的颜色却以一种我无法理解的方式改变了。如果你能帮我,我将不胜感激。

    这是我写的代码

    -(void)saveCurrentScreenToPhotoAlbum {
        int width = 768;
        int height = 1024;
    
    
        //glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        //glClear(GL_COLOR_BUFFER_BIT);
    
    
        NSInteger myDataLength = width * height * 4;
        GLubyte *buffer = (GLubyte *) malloc(myDataLength);
        GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
        glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    
        NSLog(@"%d", buffer[0]); 
        for(int y = 0; y <height; y++) {
            for(int x = 0; x <width * 4; x++) {
    
                if (buffer[y * 4 * width + x]==0) {
                    buffer[y * 4 * width + x] = 250;
                    //buffer[y * 4 * width + x+1] = 250;
                    NSLog(@" = %i",y * 4 * width + x);
    
                }
    
                    buffer2[(height - 1 - y) * width * 4 + x] = buffer[y * 4 * width + x];
    
    
    
               // printf("%d %d %d\n",buffer[(height - 1 - y) * width * 4 + x],buffer[1],buffer[2]);
            }
        }
        free(buffer);    // YOU CAN FREE THIS NOW
    
        CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, releaseData);
        int bitsPerComponent = 8;
        int bitsPerPixel = 32;
        int bytesPerRow = 4 * width;
        CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
        CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
        CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
        CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
    
        CGColorSpaceRelease(colorSpaceRef);  // YOU CAN RELEASE THIS NOW
        CGDataProviderRelease(provider);     // YOU CAN RELEASE THIS NOW
    
        UIImage *image = [[UIImage alloc] initWithCGImage:imageRef];    // change this to manual alloc/init instead of autorelease
        CGImageRelease(imageRef);    // YOU CAN RELEASE THIS NOW
    
    
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);   // add callback for finish saving
    
    }
    

    沙尼

    1 回复  |  直到 12 年前
        1
  •  1
  •   David M.    16 年前

    在保存期间更改图形代码的行为:

    if(willSave)
        glClearColor(1,1,1,1);
    else
        glClearColor(0,0,0,0);