0

first of all i want to say that i'm newbie in objective-c.

till know i have build an app that simply draws on one image (which is .png image that has 3 colors: red green blue). the applications draws lines in all over the image.

what i want is to draw lines in only one color (ex: red), even if the touch (mouse) goes up the other colors. (... as I read on internet, it should be done using flood-fill algorithm)

can you give me some ideas on how to to this.

the code that i'm using now:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;

UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:self.view];

UIGraphicsBeginImageContext(CGSizeMake(320, 568));
[drawImage.image drawInRect:CGRectMake(0, 0, 320, 568)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 2, 0, 2);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());


[drawImage setFrame:CGRectMake(0, 0, 320, 568)];
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;

[self.view addSubview:drawImage];
}

thnx in advance.

Florjon
  • 3,569
  • 3
  • 22
  • 29
  • [This post](http://stackoverflow.com/a/6064453/581994) gives some clues as to how to "open" an image and access the pixels. – Hot Licks Nov 08 '12 at 01:09
  • 1
    `CGContextRef context = UIGraphicsGetCurrentContext();` would make that a lot more readable – trapper Nov 08 '12 at 01:09

1 Answers1

0

You can create custom class derived from UIView and override message drawRect:. Then put your draw code to drawRect:. Add this custom class as subview above your UIImageView.

#import <QuartzCore/QuartzCore.h>
@interface YourView : UIView {
    ...
}


in .m file
- (void)drawRect:(CGRect)rect{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //your drawings
}
sunkehappy
  • 8,970
  • 5
  • 44
  • 65