tags:

views:

48

answers:

1
+1  Q: 

set pixel problem

how to use setpixel function in iphone

Thanks in advance

A: 

If you're drawing a UIView, I don't believe there is any way to set an individual pixel. Instead, try using CoreGraphics by overriding your UIView's drawRect: method:

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect pixelRect;
    pixelRect.x = 100; // or whatever position needs a pixel drawn
    pixelRect.y = 100;
    pixelRect.width = 1;
    pixelRect.height = 1;
    CGContextSetRGBFillColor(ctx,1,1,1,1.0); // just fill with a white color
    CGContextFillRect(ctx, pixelRect);

Another approach may be to consider using OpenGL and just drawing points that way:

    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    glOrtho (0, rect.size.width, rect.size.height, 0, 0, 1); // set to the drawing              rectangle's size
    glDisable(GL_DEPTH_TEST);
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.375, 0.375, 0);
    glClear(GL_COLOR_BUFFER_BIT);

    // draw the actual point
    glBegin(GL_POINTS);
         glColor3f(1.0f, 1.0f, 1.0f); // white color
         glVertex2f(x, y);
    glEnd();
drewh