开发者

replace specific color in openGl drawing in xcode?

i building a drawing app that uses openGl. i am new to openGL but managed to build the basic app.

I am now working on the ability of the user to save the drawings to camera roll.

i have a view that holds an image that the user uses to draw, so it have to be visible but not affected from the drawing canvas.

a bove it i have a view that i use for the drawing. because of what i said before i have set the background to black transparent with -

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

开发者_开发百科

The problem -

everything is great until i am saving the image. obviously the background color is transparent black so the images have black background while i need it to be white.

i taught to change all the black color in the canvas to white before the saving.

can someone direct me with that?

tanks shani


Hi david thanks for replying. In the first time I actually used it as is. I red a little bit more and added the rest of the code (I hope so) this time the background became white but the drawn lines disappeared. I tried to check the buffer color at each point and replace it to 255 every time it's 0. That worked great, the background became white and the lines remained but their colors changed in a way I can not understand. I will appreciate if you can help me with that.

This is the code I wrote

-(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

}

Shani


Change the behavior of the drawing code during saving:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜