2017-08-08 55 views
0

你好,我目前有这种方法在UIImageView上画线。在UIImage上绘制线NOT UIImageView - Xamarin iOS

但是我试图使它与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); 
} 

任何意见在这将是巨大的。由于

回答

2

您可以在图像上画一条线是这样的:

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

此代码将创建一个新的图像作为原始图像的大小,然后绘制原始图像的复制到了新的图像,绘制新图像上的一行。