Как мне обводить путь CGContext с несколькими случайными изображениями - PullRequest
1 голос
/ 22 декабря 2009

Я хочу обвести путь 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();
         }
     }
}

1 Ответ

1 голос
/ 30 декабря 2009

Вы не очень ясно объяснили, что вы пытаетесь сделать. Вы используете слово «инсульт» при обращении к изображениям. Вы можете обводить путь, но не с изображением. Это не имеет никакого смысла. Вы можете использовать изображение в качестве маски и путь, который указывает, какую часть изображения маскировать. Вы можете сделать это с 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).

...