1

我想CGContext使用三个或四个图像来描边路径,但在绘制路径时随机循环显示图像。我想画图像INSTEAD!画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();
         }
     }
}
4

1 回答 1

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)。

于 2009-12-29T21:55:56.783 回答