我如何中风多个随机图像的CGContext上路径(How do I stroke a CGConte

2019-10-18 19:18发布

我想中风CGContext的影像使用三个或四个图像路径,而是随机循环为我画的路径。 我想,而不是绘制图像! 绘制的CGContext现在的我。 我有这样的代码到目前为止

- (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++;

- (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();
         }
     }
}

Answer 1:

你还没有真正解释的很清楚你正在尝试做的。 您正在使用这个词“中风”参照图像。 您可以中风的路径,但不能图像。 它没有任何意义。 您可以使用图像作为掩模和指定图像的一部分,以掩盖它的路径。 你可以做到这一点与CAShapeLayer 。

如果你正在尝试一种新的点添加到路径上的每个触摸,您只需调用第一的touchesBegan这两件事:

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

然后调用

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

在后续调用-touchesBegan。 同样,我不知道正是你正在尝试做的。

有一点要注意的是,这是没有做任何事情:

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

而且我没有看到您的mouseSwiped变量有朝一日能切换(设置为NO)。



文章来源: How do I stroke a CGContext path with multiple random images