Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions src/Graphics/src/Graphics/Platforms/iOS/UIImageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,40 @@ internal static UIImage ResizeImageSource(this UIImage sourceImage, nfloat maxWi

public static UIImage ScaleImage(this UIImage target, CGSize size, bool disposeOriginal = false)
{
#pragma warning disable CA1416 // Preserve the existing rendering behavior while these unsupported APIs are replaced.
UIGraphics.BeginImageContext(size);
target.Draw(new CGRect(CGPoint.Empty, size));
var image = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
#pragma warning restore CA1416
if (!(size.Width > 0) || !(size.Height > 0) ||
double.IsInfinity(size.Width) || double.IsInfinity(size.Height))
{
return target;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 AI-Generated Review (multi-model)

[minor] Public API Surface Design — This early return hands back the same instance and silently ignores disposeOriginal: true. That is the right runtime behaviour (you cannot dispose what you are returning), and it matches the pre-existing shape at lines 11-14/29, but it is now frozen as intended behaviour by a new test, so it is worth making explicit.

The ownership hazard is concrete one level up: PlatformImage.Downsize wraps the result in a new PlatformImage (Platforms/iOS/PlatformImage.cs:25,31), so a caller passing disposeOriginal: true ends up with two IImage instances aliasing one undisposed UIImage while believing the original was released — the exact expectation Issue21886 asserts against (original.Width must throw ObjectDisposedException). Unreachable from Downsize today because the float overload guards first, hence minor. An XML <remarks> on the public method stating that disposeOriginal is ignored when the original is returned unchanged would close the contract gap without any behaviour change.

}

var width = checked((int)Math.Ceiling(size.Width));
Comment thread
PureWeen marked this conversation as resolved.
var height = checked((int)Math.Ceiling(size.Height));

using var colorSpace = CGColorSpace.CreateDeviceRGB();
using var context = new CGBitmapContext(
IntPtr.Zero,
width,
height,
8,
checked(4 * width),
colorSpace,
CGBitmapFlags.ByteOrder32Little | CGBitmapFlags.PremultipliedFirst);

context.TranslateCTM(0, height);
context.ScaleCTM(1, -1);

UIGraphics.PushContext(context);
try
{
target.Draw(new CGRect(CGPoint.Empty, size));
}
finally
{
UIGraphics.PopContext();
}

using var cgImage = context.ToImage();
var image = UIImage.FromImage(cgImage, 1, UIImageOrientation.Up);

if (disposeOriginal)
{
Expand Down
23 changes: 23 additions & 0 deletions src/Graphics/tests/DeviceTests/Tests/ImageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ public void ScaleImageUsesOneXBackingScale(float sourceScale)
Assert.Equal(5, (int)scaled.CGImage.Height);
}

[Theory]
[InlineData(0, 10)]
[InlineData(10, 0)]
[InlineData(-1, 10)]
[InlineData(10, -1)]
[InlineData(double.NaN, 10)]
[InlineData(10, double.NaN)]
[InlineData(double.PositiveInfinity, 10)]
[InlineData(10, double.PositiveInfinity)]
public void ScaleImageReturnsOriginalForNonPositiveSize(double width, double height)
Comment thread
Vignesh-SF3580 marked this conversation as resolved.
{
using var source = CreatePatternImage(UIImageOrientation.Up);

var scaled = source.ScaleImage(new CGSize(width, height), disposeOriginal: true);

Assert.Same(source, scaled);
Comment thread
Vignesh-SF3580 marked this conversation as resolved.
Comment thread
Vignesh-SF3580 marked this conversation as resolved.
Assert.NotNull(scaled.CGImage);
}

[Fact]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 TestingScaleImageCanRunOnBackgroundThread is fully subsumed by ScaleImageDoesNotRequireMainThreadProgress: the latter already invokes ScaleImage through Task.Run, then adds the stricter condition that the main thread is synchronously blocked. Keeping both adds Apple device-test execution and maintenance without covering a distinct behavior. Consider removing this simpler test.

Flagged by: 3/3 reviewers

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks — agreed that the stricter test also reaches ScaleImage through Task.Run. I am keeping the focused case on this already-proven head because it isolates the basic off-main-thread contract and verifies the resulting 1x scale directly, while the deadlock test intentionally couples execution to a synchronously blocked main queue. That separation gives a narrower failure signal if either contract regresses; treating this as the non-blocking scope tradeoff noted in the review.

public async Task ScaleImageCanRunOnBackgroundThread()
{
Expand Down Expand Up @@ -162,6 +181,10 @@ public void ScaleImageMatchesUIKitRendering(UIImageOrientation orientation)

Assert.Equal(UIImageOrientation.Up, actual.Orientation);
Assert.Equal(GetPixelData(expected), GetPixelData(actual));
Assert.Equal(expected.CGImage.Width, actual.CGImage.Width);
Assert.Equal(expected.CGImage.Height, actual.CGImage.Height);
Assert.Equal(13, (double)actual.Size.Width);
Assert.Equal(9, (double)actual.Size.Height);
}

private static UIImage CreatePatternImage(UIImageOrientation orientation)
Expand Down
Loading