我有一個應用程序,用戶可以在一個加載的圖像上自由繪製。他選擇一種鉛筆顏色,並開始繪製圖像。Xamarin iOS - 縮放後繪製圖像
除此之外,他還可以通過手指縮放圖像,這就是我遇到問題的地方。
縮放後,繪製到圖像時,它變得模糊,奇怪收縮..
代碼縮放:
void ScaleImage (UIPinchGestureRecognizer gestureRecognizer)
{
AdjustAnchorPoint (gestureRecognizer);
if (gestureRecognizer.State == UIGestureRecognizerState.Began || gestureRecognizer.State == UIGestureRecognizerState.Changed) {
gestureRecognizer.View.Transform *= CGAffineTransform.MakeScale (gestureRecognizer.Scale, gestureRecognizer.Scale);
gestureRecognizer.Scale = 1;
}
}
代碼繪製:
public override void TouchesBegan (NSSet touches, UIEvent evt)
{
base.TouchesBegan (touches, evt);
if (!canDraw)
return;
UITouch touch = (UITouch)touches.AnyObject;
lastPoint = touch.LocationInView (MyImageView);
}
public override void TouchesMoved (NSSet touches, UIEvent evt)
{
base.TouchesMoved (touches, evt);
if (!canDraw)
return;
UITouch touch = (UITouch)touches.AnyObject;
PointF currentPoint = touch.LocationInView (MyImageView);
UIGraphics.BeginImageContext (MyImageView.Frame.Size);
MyImageView.Image.Draw (new RectangleF (
0, 0,
MyImageView.Frame.Size.Width,
MyImageView.Frame.Size.Height));
CGContext ctx = UIGraphics.GetCurrentContext();
ctx.SetLineCap (CGLineCap.Round);
ctx.SetLineWidth (5.0f);
ctx.SetRGBStrokeColor (red, green, blue, 1);
CGPath path = new CGPath();
path.MoveToPoint(MyImageView.Transform, lastPoint.X, lastPoint.Y);
path.AddLineToPoint (MyImageView.Transform, currentPoint.X, currentPoint.Y);
ctx.AddPath (path);
ctx.DrawPath (CGPathDrawingMode.Stroke);
MyImageView.Image = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
lastPoint = currentPoint;
}
在此先感謝!
非常感謝您的回答。它絕對幫助我。我還想補充一點,我得到了一些來自畫線的瘋狂的尖峯,我可以通過在'TouchesBegan'內的'UIBezierPath'對象中包含以下屬性來解決這個問題:'UIBezierPath path = new UIBezierPath { LineJoinStyle = CGLineJoin.Round, LineCapStyle = CGLineCap.Round };' – hvaughan3
不是我懷疑你,但根據Bezier路徑文檔,它只是標準CGPath相關功能的包裝,所以它可以做什麼,你可以CGPath已經做不到了? –