2009-09-29 41 views
0

我想只得到那些不规则形状的像素。使用核心图形不openGL.here是一个图像,我画了一个不规则的形状。如何从iphone中的uiimageview中获取不规则形状内的像素?

alt text http://i33.tinypic.com/303enio.jpg

,这里是绘图代码

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    if(drawLineClicked) 
    { 
     UITouch *touch = [touches anyObject]; 
     if ([touch view] == EditImageView) 
     { 
     lastPoint = [touch locationInView:self.view]; 
     [self drawLines:10.0 andColorWithRed:1.0 Green:0.0 Blue:0.0 Alpha:1.0]; 
     [self.view setNeedsDisplay]; 
     currentPoint = lastPoint; 
     } 
    } 
} 


-(void)drawLines:(CGFloat)withWidth andColorWithRed:(CGFloat)red Green:(CGFloat)green Blue:(CGFloat)blue Alpha:(CGFloat)alpha 
{ 
UIGraphicsBeginImageContext(Image.size); 
    [EditImageView.image drawInRect:CGRectMake(0, 0, Image.size.width, Image.size.height)]; 
ctx = UIGraphicsGetCurrentContext(); 
CGContextSetLineCap(ctx, kCGLineCapRound); 
CGContextSetAllowsAntialiasing(ctx,TRUE); 
CGContextFlush(ctx); 
//sets the line width for a graphic context 
CGContextSetLineWidth(ctx,withWidth); 
//set the line colour 
CGContextSetRGBStrokeColor(ctx, red, green, blue, alpha); 
CGContextMoveToPoint(ctx, currentPoint.x, currentPoint.y);  
CGContextAddLineToPoint(ctx, lastPoint.x,lastPoint.y);  
CGContextStrokePath(ctx); 
EditImageView.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
} 

回答

3

这里只是折腾出了一个主意:

而不是直接绘制红线的图像,我会复制图像,将其作为新图层添加到原始图像的顶部,并为其提供一个完全透明的蒙版(因此新图层“不可见”)。

然后,当用户绘制红色线条时,使用它在不可见图层上建立一个路径作为蒙版。路径完成后,用黑色(在蒙版上)填充路径,使该部分完全不透明。你也许可以将顶层(被蒙版)调整为绘制路径的边界矩形。

最上面的图层上的不透明像素将成为被绘制的路径所限制的像素,然后您可以对其进行操作(将其绘制到新的UIImage等)。

希望是有道理的......

+1

请问您可以发布此示例源码 – 2009-09-29 16:10:12

0

看看我的老代码示例项目位置:Cropped Image

它是可可,可可不能触摸,但这个想法是一样的。您可以通过使用SourceIn合成模式进行合成来裁剪图像。

+0

您的示例代码非常好。可以告诉我如何在iphone上显示颜色托盘然后选择颜色 – 2009-09-30 06:39:42

+0

我有点惊讶,似乎没有UIKit中的颜色选择器。我想你必须写一个。 – NSResponder 2009-09-30 12:34:48