你好,我目前有这种在 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 NOT UIImageView 上画线 - Xamarin iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45577657/