0

I have this code to colour with a simply line:

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

UITouch *touch = [touches anyObject];   
CGPoint currentPoint = [touch locationInView:drawImage];

UIGraphicsBeginImageContext(drawImage.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 15.0);


//eraser
/*
if (eraser){
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 0, 0, 0);
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
}*/

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 0, 0, 1.0); //black

CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

lastPoint = currentPoint;
}

with this code I'm able to color in a view or imageview; I can choose color and size; but now I want to use a specific pattern to color this view; a specific png to use when touchmoved is called; can you help me?

cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241

2 Answers2

2

Check out https://stackoverflow.com/a/707329/108574 if you want to draw pattern (tiled images).

However, you are doing it the wrong way in your code. You aren't supposed to draw to the graphics context during UI events. Drawing routines should be inside - (void)drawRect:(CGRect)rect of a UIView instance. When a UI event refreshes graphics, you record the new state somewhere, and then issue - (void)setNeedsDisplay message to the UIView instance to redraw.

Community
  • 1
  • 1
He Shiming
  • 5,710
  • 5
  • 38
  • 68
  • I don't want draw a tile image, but I want to draw an image with my finger; my code draw a line; instead I want draw an image (png at example) – cyclingIsBetter Mar 20 '12 at 17:13
  • Sorry I misunderstood. You can use either ``CGContextDrawTiledImage`` or ``UIImage:drawInRect`` to draw images at particular locations. But my previous suggestion still stands. You'd better record points you'd like to paint during the UI event (use NSArray to store points), then loop over the points and draw images in ``drawRect`` – He Shiming Mar 20 '12 at 17:19
1

Try colorWithPatternImage:

Creates and returns a color object using the specified image.

+ (UIColor *)colorWithPatternImage:(UIImage *)image

(reference)

Frade
  • 2,938
  • 4
  • 28
  • 39