From 67b6d29aa1f8734baf0c58eeb0c76f64eafbe95b Mon Sep 17 00:00:00 2001 From: Gerald Versluis Date: Mon, 10 Jan 2022 12:04:20 +0100 Subject: [PATCH] Fix NRE when using Line on iOS with Xcode 13.2 (#14993) * Update ShapeRenderer.cs * Update ShapeRenderer.cs --- .../Shapes/ShapeRenderer.cs | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/Xamarin.Forms.Platform.iOS/Shapes/ShapeRenderer.cs b/Xamarin.Forms.Platform.iOS/Shapes/ShapeRenderer.cs index 89b2add418d..4386da30bfc 100644 --- a/Xamarin.Forms.Platform.iOS/Shapes/ShapeRenderer.cs +++ b/Xamarin.Forms.Platform.iOS/Shapes/ShapeRenderer.cs @@ -386,8 +386,18 @@ void BuildRenderPath() viewBounds.Width -= _strokeWidth; viewBounds.Height -= _strokeWidth; - nfloat widthScale = viewBounds.Width / _pathFillBounds.Width; - nfloat heightScale = viewBounds.Height / _pathFillBounds.Height; + if (viewBounds.Width < 0) + viewBounds.Width = 0; + + if (viewBounds.Height < 0) + viewBounds.Height = 0; + + var calculatedWidth = viewBounds.Width / _pathFillBounds.Width; + var calculatedHeight = viewBounds.Height / _pathFillBounds.Height; + + nfloat widthScale = nfloat.IsNaN(calculatedWidth) ? 0 : calculatedWidth; + nfloat heightScale = nfloat.IsNaN(calculatedHeight) ? 0 : calculatedHeight; + var stretchTransform = CGAffineTransform.MakeIdentity(); switch (_stretch) @@ -441,11 +451,15 @@ void BuildRenderPath() if (_pathStrokeBounds.Width > Bounds.Width) width = Bounds.Width - adjustX; - if (_pathStrokeBounds.Height > Bounds.Height) + if (_pathStrokeBounds.Height > Bounds.Height) height = Bounds.Height - adjustY; Frame = new CGRect(adjustX, adjustY, width, height); - var transform = new CGAffineTransform(Bounds.Width / width, 0, 0, Bounds.Height / height, -adjustX, -adjustY); + + var calculatedWidth = Bounds.Width / width; + var calculatedHeight = Bounds.Height / height; + + var transform = new CGAffineTransform(nfloat.IsNaN(calculatedWidth) ? 0 : calculatedWidth, 0, 0, nfloat.IsNaN(calculatedHeight) ? 0: calculatedHeight, -adjustX, -adjustY); _renderPath = _path.CopyByTransformingPath(transform); } else