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

发布于 2024-08-05 22:42:02 字数 1470 浏览 4 评论 0原文

我只想获取不规则形状内的像素。使用核心图形而不是 openGL。这是我绘制不规则形状的图像。

替代文本

这是绘图代码

- (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();
}

i want to get only those pixels which are inside an irregular shape..using core graphics not openGL.here is an image where i have drawn an irregular shape.

alt text

and here is the drawing code

- (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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七分※倦醒 2024-08-12 22:42:02

这里只是提出一个想法:我

不是直接在图像上绘制红线,而是复制图像,将其作为新图层添加到原始图像之上,并给它一个完全透明的蒙版(因此新图层是“看不见的”)。

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

最顶层的不透明像素将是由绘制路径界定的像素,然后您可以对其执行您喜欢的操作(将其绘制到新的 UIImage 等)。

希望这是有道理的...

Here's just tossing out an idea:

Instead of drawing the red line directly on the image, I'd duplicate the image, add it as a new layer on top of the original image, and give it a totally transparent mask (so the new layer is "invisible").

Then when the user draws the red line, use that to build a path as a mask on the invisible layer. When the path is finished, fill in the path with black (on the mask), to make that portion totally opaque. You could maybe then resize the top layer (that's been masked) to be the bounding rectangle of the drawn path.

The opaque pixels on the topmost layer will then be the ones that were bounded by the drawn path, and you can then do what you please with it (draw it to a new UIImage, etc).

Hope that makes sense...

感悟人生的甜 2024-08-12 22:42:02

在这里查看我的旧示例代码项目:裁剪图像

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

Take a look at my old sample code project here: Cropped Image

It's for Cocoa, not Cocoa Touch, but the idea is the same. You can crop an image by compositing using the SourceIn compositing mode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文