Monotouch Draw line at UIImage

Using Monotouch, how would I draw a simple line on an image in a UIImage that is currently being displayed?

A simple example, of course, is beautiful.

Obj-C's answer is ok too.

+3
source share
1 answer

Assuming you have a custom UIImageView with an image set in its Image object, put 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.

+3
source

Source: https://habr.com/ru/post/1786860/


All Articles