绘制到 UIImage 中

2024-03-30

如何使用 monotouch 绘制现有的 UIImage?

我加载图像: UIImage.FromFile("MyImage.png")

然后我想在这个图像中绘制一条字符串和一些线条。

有人有代码示例吗?

Thx


这是一个执行此操作的方法:

private void drawOnTouch(object data)
{

    UITouch touch = data as UITouch;

    if (null != touch)
    {

        UIGraphics.BeginImageContext(this.Image == null ? this.Frame.Size : this.Image.Size);

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

            if (this.Image != null)
            {

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


            } //end if

            PointF lastLocation = touch.PreviousLocationInView(this);
            PointF pt = touch.LocationInView(this);
            using (CGPath path = new CGPath())
            {

                cont.SetLineCap(CGLineCap.Round);
                cont.SetLineWidth(3);
                cont.SetRGBStrokeColor(0, 2, 3, 1);
                path.MoveToPoint(lastLocation.X, lastLocation.Y);
                path.AddLines(new PointF[] { new PointF(lastLocation.X, 
                                lastLocation.Y),
                            new PointF(pt.X, pt.Y) });
                path.CloseSubpath();

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

            }//end using path


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

    }//end if


}//end void drawOnTouch

如果将此方法放在 UIImageView 的子类中并从 TouchesBegan 和 TouchesMoved 调用它,当您触摸屏幕时,它将在图像上绘制。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

绘制到 UIImage 中 的相关文章

随机推荐