2010-08-05 71 views
3

我是一名初學者,在iphone上進行繪畫應用。如何在UIImageView上繪製噴槍?

爲我的iPhone應用程序稱爲噴槍......

將噴上的UIImageView添加新工具。任何人都可以幫助我解決如何使用它。

+0

你說的粉筆線是什麼意思? – Plumenator 2010-08-05 07:20:19

+0

感謝plumenator重播.... 其實我想在我的UIImageView上畫畫筆... – kiran 2010-08-05 07:24:38

回答

0

我想你可能在尋找CGContextBeginPath及其相關功能。我不太確定如何定義一個新的筆劃,但我想它可以用類似[UIColor colorFromImage:myImage]的東西來處理。你應該看看Quartz 2D,試着看看here

/托馬斯

+0

謝謝托馬斯..... 有沒有什麼方法可以像粉筆畫線。 – kiran 2010-08-05 10:54:36

+1

這不會是在公園散步。您應該可以使用Quartz 2D API以某種方式製作自定義筆(筆畫)。如果你無法在API中找到這個功能,我最好的猜測就是你可以加載一個圖像(白色的粉筆),並在用戶觸摸圖像時不斷將其繪製到圖像上。您將不得不在UIResponder上使用'touchesBegan:withEvent:',或者您可以製作一個手勢識別器:http://developer.apple.com/iphone/library/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizers/GestureRecognizers。 HTML – 2010-08-07 00:44:56

+0

感謝......托馬斯.... – kiran 2010-08-07 15:23:08

6
Logic for air brush......... 

- (UIBezierPath *)pathFromPoint:(CGPoint)start toPoint:(CGPoint)end { 

    CGFloat lineWidth=10; 
    redrawRect = CGRectMake(end.x-lineWidth,end.y-lineWidth,lineWidth*2,lineWidth*2); 
    UIBezierPath *bezierPath = [UIBezierPath bezierPath]; 
    UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:redrawRect]; 
    NSInteger i, x, y; 

    NSInteger modNumber =4*(int)lineWidth; 
    for (i = 0; i < (lineWidth*lineWidth)/2; i++) { 
     do { 
      x = (random() % modNumber)+end.x - 2*lineWidth; 
      y = (random() % modNumber)+end.y - 2*lineWidth; 
     } while (![circle containsPoint:CGPointMake(x,y)]); 

     [bezierPath appendPath:[UIBezierPath bezierPathWithRect:CGRectMake(x,y,0.5,0.5)]]; 
    } 
    return bezierPath; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    currentPoint = [touch locationInView:self.view]; 
    currentPoint.y -=20; 
    [self drawCircle]; 
} 
-(void)drawCircle{ 
    UIGraphicsBeginImageContext(self.drawImage.frame.size); 
    [drawImage.image drawInRect:CGRectMake(0,0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)]; 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(),10); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0); 
    UIBezierPath *path=[self pathFromPoint:currentPoint 
            toPoint:currentPoint]; 
    [path stroke]; 
    lastPoint = currentPoint; 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    CGContextFlush(UIGraphicsGetCurrentContext()); 
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
} 
+0

在drawCircle方法中,也許你的意思是:UIBezierPath * path = [self pathFromPoint:lastPoint toPoint:currentPoint]; ? – Quentin 2011-04-06 15:42:17

+0

沒有兩個都是同樣的點,立刻噴上! – kiran 2012-02-28 21:04:31

相關問題