剪掉图像的一部分

时间:2013-08-27 13:19:52

标签: c# ios xamarin.ios xamarin

我想剪掉一部分图片。 我的计划是创建一个带有图像的图层,但图像会溢出图层。

它看起来应该是这样的例子:

the cutted image

有什么想法吗?感谢。

using (CGLayer starLayer = CGLayer.Create (g, rect.Size)) {
            // set fill to blue
            starLayer.Context.SetFillColor (0f, 0f, 1f, 0.4f);
            starLayer.Context.AddLines (myStarPoints);

            starLayer.Context.AddQuadCurveToPoint(this.points [3].X, this.points [3].Y, this.points [2].X, this.points [2].Y);
            starLayer.Context.AddQuadCurveToPoint(this.points [5].X, this.points [5].Y, this.points [4].X, this.points [4].Y);
            starLayer.Context.FillPath ();



            double width =  Math.Abs((double)(this.points [1].X - this.points [0].X));
            float prop = this.tmpimage.Size.Width / (float)width;

            float height = (this.tmpimage.Size.Height / prop);

            double centerteeth = (Math.Sqrt (Math.Pow ((this.points [4].X - this.points [2].X), 2.0) + Math.Pow ((this.points [4].Y - this.points [2].Y), 2.0))) / 2 - (width/2);



            starLayer.Context.DrawImage (new RectangleF ((float)centerteeth + this.points[2].X, this.points [2].Y, (float)width, height), this.imagerotated.CGImage);


            // draw the layer onto our screen
            float starYPos = 5;
            float starXPos = 5;


            g.DrawLayer (starLayer, new PointF (starXPos, starYPos));



        }

2 个答案:

答案 0 :(得分:0)

如果您正在寻找快速解决方案来完成它,您可以使用两个图像来完成您的需求吗? (黑色图像,然后是背景)将一个UIImageView放在另一个上面?

由于两个原因,我通常只会使用CoreGraphics

  • 没有其他方法可以实现我的目标
  • 使用普通UIViews不够高效

答案 1 :(得分:0)

现在我找到了一个对我有用的解决方案,无论如何,谢谢你的帮助。

图像子层可以在主图层中移动。

这就是我的解决方案:

        CALayer imageLayer = new CALayer (Layer);
        imageLayer.Frame = Layer.Bounds;
        imageLayer.BackgroundColor = UIColor.Black.CGColor;


        CAShapeLayer maskLayer = new CAShapeLayer();
        maskLayer.FillRule = CAShapeLayer.FillRuleEvenOdd;
        maskLayer.Frame = Frame;


        double width =  Math.Abs((double)(this.points [1].X - this.points [0].X));
        float prop = this.tmpimage.Size.Width / (float)width;
        float height = (this.tmpimage.Size.Height / prop);


        CALayer lay = new CALayer ();
        lay.Bounds = new RectangleF (0, 0, (float)width, height);
        lay.Position = this.points[6];

        lay.Contents = this.tmpimage.CGImage;
        layers [6] = lay;

        imageLayer.AddSublayer (lay);



        CGPath p1 = new CGPath();

        p1.MoveToPoint (this.points [2]);
        p1.AddQuadCurveToPoint(this.points [3].X, this.points [3].Y, this.points [4].X, this.points [4].Y);
        p1.AddQuadCurveToPoint(this.points [5].X, this.points [5].Y, this.points [2].X, this.points [2].Y);

        maskLayer.Path = p1;

        imageLayer.Mask = maskLayer;
        Layer.AddSublayer (imageLayer);


        CAShapeLayer pathLayer1 = new CAShapeLayer ();
        pathLayer1.Path = maskLayer.Path;
        pathLayer1.LineWidth = 0f;
        pathLayer1.StrokeColor = UIColor.Clear.CGColor;
        pathLayer1.FillColor = UIColor.Clear.CGColor;
        Layer.AddSublayer (pathLayer1);