[英]Cut out part of an 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));
}
如果您正在尋找一種快速的解決方案來完成它,可以使用兩個圖像來完成所需的工作嗎? (一個黑色的圖像,然后是背景)將一個UIImageView
放在另一個之上?
我通常僅出於以下兩個原因而開始使用CoreGraphics
:
UIViews
性能不夠 現在,我找到了一個對我有用的解決方案,無論如何,謝謝您的幫助。
imagessublayer可在主層中移動。
那就是我的解決方案:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.