2017-08-08 1 views
0

Bonjour, J'ai actuellement cette méthode qui dessine une ligne sur un UIImageView.Ligne de dessin sur un UIImage NOT UIImageView - Xamarin iOS

Cependant j'essaye de le rendre compatible avec un UIImage et n'ai pas eu de chance. This example here fonctionne magnifiquement pour le texte mais pas idéal pour les lignes.

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

Tous les conseils sur ce serait génial. Merci

Répondre

2

Vous pouvez dessiner une ligne sur l'image comme ceci:

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

Ce code va créer une nouvelle image que la taille de l'image originale, puis dessinez une copie de l'image originale sur la nouvelle image et d'en tirer une ligne sur la nouvelle image.