2

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

它应该看起来像这个例子:

剪切的图像

有任何想法吗?谢谢。

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



        }
4

2 回答 2

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);
于 2013-08-28T14:56:36.630 回答
0

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

我通常只是CoreGraphics出于两个原因才开始使用:

  • 没有其他方法可以实现我的目标
  • 使用普通UIViews的性能不够
于 2013-08-28T11:48:51.790 回答