Repository navigation
[net11.0] Make iOS image scaling deadlock-safe #37057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 14 commits
ade78c6
988f409
fa6a4ab
82a3d6e
7151b52
560bc61
160475b
63c54b1
476929f
0a1fc0c
611e859
a4c742b
a47c914
d11f043
25f54ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,12 +56,39 @@ 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) | ||
|
Vignesh-SF3580 marked this conversation as resolved.
Resolved
|
||
| { | ||
| return target; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[minor] Public API Surface Design — This early return hands back the same instance and silently ignores The ownership hazard is concrete one level up: |
||
| } | ||
|
|
||
| var width = checked((int)Math.Ceiling(size.Width)); | ||
|
PureWeen marked this conversation as resolved.
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.PremultipliedFirst); | ||
|
Vignesh-SF3580 marked this conversation as resolved.
Resolved
|
||
|
|
||
| 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) | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -84,6 +84,20 @@ 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)] | ||
| public void ScaleImageReturnsOriginalForNonPositiveSize(double width, double height) | ||
|
Vignesh-SF3580 marked this conversation as resolved.
Resolved
|
||
| { | ||
| using var source = CreatePatternImage(UIImageOrientation.Up); | ||
|
|
||
| var scaled = source.ScaleImage(new CGSize(width, height), disposeOriginal: true); | ||
|
|
||
| Assert.Same(source, scaled); | ||
|
Vignesh-SF3580 marked this conversation as resolved.
Resolved
Vignesh-SF3580 marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| [Fact] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Testing — Flagged by: 3/3 reviewers
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks — agreed that the stricter test also reaches |
||
| public async Task ScaleImageCanRunOnBackgroundThread() | ||
| { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.