Search code examples
iosobjective-ccolorspixel

iOS - change one pixel of a view


In my app I need to change the color of one pixel(of a view) to black, I need to do this in the (void)touchesMoved:withEvent: of a custom gester recognizer, which will be applied on the view. (I am making a pen like thing).

My question is what is the simplest draw a line behind the gester recognizer, the line would stay after the gester recognizer is moved/

Let me know if you need any more information.


Solution

  • Drawing in a view is done in drawRect: http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-BBCDGJHF

    You'll need to set an instance variable or property in touchesMoved to the point that you need to paint then call [self setNeedsDisplay] and drawRect will get invoked. In drawRect you will draw a one pixel rectangle.

    Something like this, modify to suit your needs:

    - (void)drawRect:(CGRect)rect {    
        [super drawRect:rect];  
        CGRect rectangle = CGRectMake(self.cachedPoint.x, self.cachedPoint.y, 1, 1);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
        CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
        CGContextFillRect(context, rectangle);
    }