ios - 在 UIImage NOT UIImageView 上画线 - Xamarin iOS

标签 ios xamarin.ios uiimageview uiimage

你好,我目前有这种在 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/

相关文章:

ios - UIImageView 使其他 View 不一致

ios - AwakeFromNib 没有导出

ios - UILabel 截断多行标签中的中间行

iphone - ios 上没有屏幕历史记录

c++ - 在 Objective-C++ 中将 C++ GUID 结构转换为 NSString*

iphone - UIImageView 带有大图像。问题

ios - 如何在 Swift 中使用 JTCalendar?

ios - 当他们已经是联系人时,使用 CallKit 识别来电者

c# - 使用 WriteImageToSavedPhotosAlbum 时未保存 Gps exif 数据

ios - "This application is modifying the autolayout engine from a background thread"