How to stroke a CGContext path with multiple random images

I want to stroke a CGContext path with three or four images, but randomly scan the images as I draw the path. I want to draw INSTEAD images !! from the CGContext picture I am now. I have this code so far

 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:self.view]; currentPoint.x = currentPoint.x; currentPoint.y = currentPoint.y; mouseSwiped = YES; UIGraphicsBeginImageContext(self.view.frame.size); [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES); CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushSizeVal); CGContextSetAlpha(UIGraphicsGetCurrentContext(), drawingColorAlpha); CGContextSaveGState(UIGraphicsGetCurrentContext()); //Probably right here CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), drawingColorRed, drawingColorGreen, drawingColorBlue, drawingColorAlpha); CGContextBeginPath(UIGraphicsGetCurrentContext()); CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); CGContextStrokePath(UIGraphicsGetCurrentContext()); drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); //drawImage is the UIImageView that I am drawing my context to NSLog(@"current point x: %d current point y: %d",currentPoint.x, currentPoint.y); //Used for my purposes lastPoint = currentPoint; mouseMoved++; 

and

 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if (!optionsDisplayerVisible && canDraw) { if(!mouseSwiped) { UIGraphicsBeginImageContext(self.view.frame.size); [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES); CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushSizeVal); CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), drawingColorRed, drawingColorGreen, drawingColorBlue, drawingColorAlpha); CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); CGContextStrokePath(UIGraphicsGetCurrentContext()); CGContextFlush(UIGraphicsGetCurrentContext()); drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); //drawImage is the UIImageView that I am drawing to UIGraphicsEndImageContext(); } } } 
+1
source share
1 answer

You did not explain very clearly what you are trying to do. You use the word "move" when referring to images. You can stroke the path, but not with the image. It makes no sense. You can use the image as a mask and a path that determines which part of the image is masked. You can do this with CAShapeLayer .

If you try to add a new point to the path with every touch, you can simply call these two things at the first touch of Began:

 CGContextBeginPath(UIGraphicsGetCurrentContext()); CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 

And then call

 CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 

on subsequent calls to -touchesBegan. Again, I donโ€™t know exactly what you are trying to do.

It should be noted that this does nothing:

 currentPoint.x = currentPoint.x; currentPoint.y = currentPoint.y; 

And I do not see where your mouseSwiped variable always switches (set to NO).

+1
source

All Articles