如何使用多个随机图像描边CGContext路径

时间:2009-12-21 23:32:41

标签: iphone random png cgcontext

我想使用三个或四个图像描边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();
         }
     }
}

1 个答案:

答案 0 :(得分:1)

你还没有真正清楚地解释你想要做什么。您在引用图像时使用“笔画”一词。您可以描边路径,但 图像。它没有任何意义。您可以使用图像作为蒙版和路径来指定要遮罩的图像部分。您可以使用CAShapeLayer执行此操作。

如果您尝试在每次触摸时向路径添加新点,您可以在第一次触摸时调用这两项内容:

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