Skip to content

Commit 7f18be9

Browse files
SyedAbdulAzeemSF4852PureWeen
authored andcommitted
[iOS, Mac] Fix for downsized image retaining original dimensions in GraphicsView (#30007)
* [iOS] Fix for downsized image retains original size * Renamed variables for better readability
1 parent 0e970ef commit 7f18be9

File tree

4 files changed

+106
-1
lines changed

4 files changed

+106
-1
lines changed

src/Controls/tests/TestCases.HostApp/Controls.TestCases.HostApp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<EmbeddedResource Include="Resources\Fonts\Dokdo-Regular.ttf" />
6969
<EmbeddedResource Include="Resources\Images\royals.png" />
7070
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
71+
<EmbeddedResource Include="Resources\Images\royals.png" />
7172
</ItemGroup>
7273

7374
<ItemGroup>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System.Reflection;
2+
using Microsoft.Maui.Graphics.Platform;
3+
using IImage = Microsoft.Maui.Graphics.IImage;
4+
5+
namespace Controls.TestCases.HostApp.Issues;
6+
7+
[Issue(IssueTracker.Github, 30006, "The downsized image continues to retain its original dimensions", PlatformAffected.iOS)]
8+
public class Issue30006 : ContentPage
9+
{
10+
Label _convertedImageStatusLabel;
11+
public Issue30006()
12+
{
13+
_convertedImageStatusLabel = new Label
14+
{
15+
AutomationId = "ConvertedImage",
16+
Text = "Result Image Status: "
17+
};
18+
19+
VerticalStackLayout verticalStackLayout = new VerticalStackLayout
20+
{
21+
Padding = new Thickness(20),
22+
Spacing = 10,
23+
Children =
24+
{
25+
CreateButton("DownSize", OnDownSize),
26+
_convertedImageStatusLabel,
27+
}
28+
};
29+
30+
Content = new ScrollView { Content = verticalStackLayout };
31+
}
32+
33+
Button CreateButton(string text, EventHandler handler)
34+
{
35+
Button button = new Button
36+
{
37+
AutomationId = $"Issue30006DownSizeBtn",
38+
Text = text,
39+
HorizontalOptions = LayoutOptions.Fill
40+
};
41+
42+
button.Clicked += handler;
43+
return button;
44+
}
45+
46+
async Task<IImage> LoadImageAsync()
47+
{
48+
var assembly = GetType().GetTypeInfo().Assembly;
49+
using var stream = assembly.GetManifestResourceStream("Controls.TestCases.HostApp.Resources.Images.royals.png");
50+
return await Task.FromResult(PlatformImage.FromStream(stream));
51+
}
52+
53+
async void OnDownSize(object sender, EventArgs e)
54+
{
55+
var image = await LoadImageAsync();
56+
var res = image.Downsize(10);
57+
58+
UpdateStatusLabels(res);
59+
}
60+
61+
void UpdateStatusLabels(IImage resultImage)
62+
{
63+
_convertedImageStatusLabel.Text = TryAccessImage(resultImage)
64+
? "Success"
65+
: "Failure";
66+
}
67+
68+
bool TryAccessImage(IImage downsizedImage)
69+
{
70+
if (Math.Round(downsizedImage.Width) == 10 && Math.Round(downsizedImage.Height) == 8)
71+
{
72+
return true;
73+
}
74+
75+
return false;
76+
}
77+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#if TEST_FAILS_ON_WINDOWS // Issue Link - https://github.com/dotnet/maui/issues/16767
2+
using NUnit.Framework;
3+
using UITest.Appium;
4+
using UITest.Core;
5+
6+
namespace Microsoft.Maui.TestCases.Tests.Issues;
7+
8+
public class Issue30006 : _IssuesUITest
9+
{
10+
public Issue30006(TestDevice device) : base(device)
11+
{
12+
}
13+
14+
public override string Issue => "The downsized image continues to retain its original dimensions";
15+
16+
[Test]
17+
[Category(UITestCategories.GraphicsView)]
18+
public void VerifyDownsizedImageHasNewDimensions()
19+
{
20+
App.WaitForElement("ConvertedImage");
21+
App.Tap("Issue30006DownSizeBtn");
22+
23+
var downsizedLabelText = App.FindElement("ConvertedImage").GetText();
24+
Assert.That(downsizedLabelText, Is.EqualTo("Success"));
25+
}
26+
}
27+
#endif

src/Graphics/src/Graphics/Platforms/iOS/UIImageExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static UIImage ScaleImage(this UIImage target, float maxWidth, float maxH
2727

2828
public static UIImage ScaleImage(this UIImage target, CGSize size, bool disposeOriginal = false)
2929
{
30-
using (var renderer = new UIGraphicsImageRenderer(target.Size))
30+
using (var renderer = new UIGraphicsImageRenderer(size))
3131
{
3232
var resultImage = renderer.CreateImage((UIGraphicsImageRendererContext imageContext) =>
3333
{

0 commit comments

Comments
 (0)