2009-12-21 32 views
1

我想使用三個或四個圖像描邊CGContext路徑,但在繪製路徑時隨機循環顯示圖像。我想繪製圖像INSTEAD!現在我畫了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(); 
     } 
    } 
} 
+0

描述你想要什麼更具體;那麼請描述你的代碼實際上在你提交的狀態下做了什麼。 – dlamblin 2009-12-25 18:32:16

+0

你是否每次都有意地在你自己的圖像上繪製自己的圖像? – 2009-12-25 18:44:10

+0

我想繪製圖像,而不是現在繪製CGContext。如果你需要更多的探索,然後問。 – Jaba 2009-12-26 00:14:21

回答

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

+0

我正在使用mouseSwiped做其他事情,但我想繪製旋轉的平鋪圖像,以便圖像看起來不像他們只是不斷繪製 – Jaba 2009-12-30 03:29:51