您好,我目前有一种在UIImageView上画线的方法。

但是,我试图使其与UIImage兼容,并且没有任何运气。 This example here对于文本来说效果很好,但对线条效果不好。

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


任何建议,这将是巨大的。谢谢

最佳答案

您可以在图像上画一条线,如下所示:

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


此代码将创建一个新图像作为原始图像大小,然后将原始图像的副本绘制到新图像上并在新图像上绘制一条线。

关于ios - 在UIImage上画线而不是UIImageView-Xamarin iOS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45577657/

10-11 04:38