开发者

Monotouch Draw line on UIImage

开发者 https://www.devze.com 2023-02-06 10:59 出处:网络
Using开发者_C百科 Monotouch, how would I draw a simple line onto the image in a UIImage that is currently being display?

Using开发者_C百科 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号