如何从iphone中的uiimageview中获取不规则形状内的像素?

问题描述 投票:0回答:2

我想只获得那些在不规则形状内的像素。使用核心图形不是openGL。这是一个我画了不规则形状的图像。

alt text

这是绘图代码

- (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();
}
iphone objective-c core-graphics bitmapdata
2个回答
3
投票

这里只是抛出一个想法:

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

然后,当用户绘制红线时,使用它来构建路径作为不可见图层上的蒙版。路径完成后,用黑色填充路径(在蒙版上),使该部分完全不透明。然后,您可以调整顶层(已被遮罩)的大小,使其成为绘制路径的边界矩形。

最顶层的不透明像素将是由绘制路径限制的像素,然后您可以随意使用它(将其绘制到新的UIImage等)。

希望有道理......


0
投票

看看我的旧示例代码项目:Cropped Image

它适用于Cocoa,而不是Cocoa Touch,但这个想法是一样的。您可以使用SourceIn合成模式通过合成裁剪图像。

© www.soinside.com 2019 - 2024. All rights reserved.