Monotouch 在 UIImage 上画线

发布于 2024-10-13 01:04:44 字数 94 浏览 0 评论 0原文

使用 Monotouch,如何在当前显示的 UIImage 中的图像上绘制一条简单的线条?

简单的例子当然可以。

Obj-C 的答案也可以。

Using Monotouch, how would I draw a simple line onto the image in a UIImage that is currently being display?

Simple example is of course fine.

Obj-C answer is ok too.

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

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

发布评论

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

评论(1

遗失的美好 2024-10-20 01:04:44

假设您有一个自定义 UIImageView,其 Image 属性上设置了图像,请将此方法放入其中:

private void DrawLineOnImage()
{

    UIGraphics.BeginImageContext(this.Image.Size);

    using (CGContext cont = UIGraphics.GetCurrentContext())
    {

        cont.TranslateCTM(0f, this.Image.Size.Height);
        cont.ScaleCTM(1.0f, -1.0f);
        cont.DrawImage(new RectangleF(0f,0f,this.Image.Size.Width, this.Image.Size.Height), this.Image.CGImage);
        cont.ScaleCTM(1.0f, -1.0f);
        cont.TranslateCTM(0f, -this.Image.Size.Height);

        using (CGPath path = new CGPath())
        {

            cont.SetLineWidth(3);
            cont.SetRGBStrokeColor(255, 0, 0, 1);
            path.AddLines(new PointF[] { 
                          new PointF(10, 10),
                            new PointF(100, 100) });
            path.CloseSubpath();

            cont.AddPath(path);
            cont.DrawPath(CGPathDrawingMode.FillStroke);
            this.Image = UIGraphics.GetImageFromCurrentImageContext();

        }//end using path


    }//end using cont
    UIGraphics.EndImageContext();

}//end void DrawLineOnImage

这会在图像本身上绘制一条红线。

Assuming you have a custom UIImageView with an image set on its Image property, place this method in it:

private void DrawLineOnImage()
{

    UIGraphics.BeginImageContext(this.Image.Size);

    using (CGContext cont = UIGraphics.GetCurrentContext())
    {

        cont.TranslateCTM(0f, this.Image.Size.Height);
        cont.ScaleCTM(1.0f, -1.0f);
        cont.DrawImage(new RectangleF(0f,0f,this.Image.Size.Width, this.Image.Size.Height), this.Image.CGImage);
        cont.ScaleCTM(1.0f, -1.0f);
        cont.TranslateCTM(0f, -this.Image.Size.Height);

        using (CGPath path = new CGPath())
        {

            cont.SetLineWidth(3);
            cont.SetRGBStrokeColor(255, 0, 0, 1);
            path.AddLines(new PointF[] { 
                          new PointF(10, 10),
                            new PointF(100, 100) });
            path.CloseSubpath();

            cont.AddPath(path);
            cont.DrawPath(CGPathDrawingMode.FillStroke);
            this.Image = UIGraphics.GetImageFromCurrentImageContext();

        }//end using path


    }//end using cont
    UIGraphics.EndImageContext();

}//end void DrawLineOnImage

This draws a red line on the image itself.

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