简体   繁体   English

在UIImage上画线而不是UIImageView-Xamarin iOS

[英]Drawing line on a UIImage NOT UIImageView - Xamarin iOS

Hello I currently have this method which draws line on an UIImageView. 您好,我目前有一种在UIImageView上画线的方法。

However I am trying to make it compatible with a UIImage and have not had any luck. 但是,我试图使其与UIImage兼容,并且没有任何运气。 This example here works beautifully for text but not great for lines. 此示例在文本上效果很好,但对于线条效果不好。

DrawOnUIImageView.cs DrawOnUIImageView.cs

 private void Draw(Face face, UIImageView imageView)
{
    CAShapeLayer boundingBoxLayer = new CAShapeLayer();
    boundingBoxLayer.Frame = face.rect;
    boundingBoxLayer.FillColor = null;
    boundingBoxLayer.StrokeColor = UIColor.Red.CGColor;
    imageView.Layer.AddSublayer(boundingBoxLayer);

    CAShapeLayer secondBoxLayer = new CAShapeLayer();
    secondBoxLayer.FillColor = null;
    secondBoxLayer.StrokeColor = UIColor.Green.CGColor;
    boundingBoxLayer.AddSublayer(secondBoxLayer);

    var path = new CGPath();
    List<LandmarkLine> lines = new List<LandmarkLine>();
    foreach (var landmark in face.landmarks)
    {
        List<CGPoint> addTo = new List<CGPoint>();
        foreach (var point in landmark.points)
        {
            addTo.Add(new CGPoint((point.X * face.rect.Width), (1 - point.Y) * face.rect.Height));
        }
        CGPath outline = new CGPath();
        outline.AddLines(addTo.ToArray());
        outline.CloseSubpath();
        path.AddPath(outline);
    }
    secondBoxLayer.Path = path;
    //imageView.Layer.AddSublayer(outline);
}

Any advice on this would be great. 任何建议,这将是巨大的。 Thanks 谢谢

You can draw a line on your image like this: 您可以在图像上画一条线,如下所示:

        private UIImage drawLineOnImage(UIImage img)
        {

            //UIImage orgImage = <YOUR IMAGE> 

            UIGraphics.BeginImageContext(orgImage.Size);

            // 1: Draw the original image as the background
            orgImage.Draw(new RectangleF(0,0,(float)orgImage.Size.Width,(float)orgImage.Size.Height));

            // 2: Draw the line on the image
            CGContext context = UIGraphics.GetCurrentContext();
            context.SetLineWidth(1.0f);
            context.MoveTo(0, 80);
            context.AddLineToPoint(orgImage.Size.Width, 80);
            context.SetStrokeColor(UIColor.Blue.CGColor);
            context.StrokePath();

            // Create new image
            UIImage image = UIGraphics.GetImageFromCurrentImageContext();

            // Tidy up
            UIGraphics.EndImageContext();

            return image;
        }

This code will create a new image as the original image size, then draw a copy of the original image onto the new image and draw a line on the new image. 此代码将创建一个新图像作为原始图像大小,然后将原始图像的副本绘制到新图像上并在新图像上绘制一条线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM