本文介绍了画入UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

how can I draw into an existing UIImage using monotouch?

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

I load an image: UIImage.FromFile("MyImage.png")

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

Then I want to draw a string and some lines into this image.

有人有代码示例吗?

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调用,则在触摸屏幕时它将在图像上绘制。

If you place this method in a subclass of UIImageView and call it from TouchesBegan and TouchesMoved, when you touch the screen it will draw on the image.

这篇关于画入UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:17