-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix FileImageSource not displaying images from absolute paths on iOS/macOS #31893
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
base: net10.0
Are you sure you want to change the base?
Conversation
/azp run |
Azure Pipelines successfully started running 3 pipeline(s). |
@copilot build error: src\Core\src\ImageSources\iOS\ImageSourceExtensions.cs(81,9): Error CS0136: A local or parameter named 'url' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter |
Fixed in commit e7334002. Removed the duplicate |
Azure Pipelines successfully started running 3 pipeline(s). |
/rebase |
…macOS Co-authored-by: jfversluis <[email protected]>
…ation Co-authored-by: jfversluis <[email protected]>
ff4a277
to
0397e61
Compare
Azure Pipelines successfully started running 3 pipeline(s). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Review
var filename = imageSource.File; | ||
|
||
// If the path is absolute (e.g., from MediaPicker), try to load it directly first | ||
if (Path.IsPathRooted(filename)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- GetPlatformImageSource() checks
File.Exists(filename)
- GetPlatformImage() does NOT
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added File.Exists()
check to GetPlatformImage()
in commit 3fefba46 for consistency with GetPlatformImageSource()
.
// If the path is absolute (e.g., from MediaPicker), check if it exists directly first | ||
if (Path.IsPathRooted(filename) && File.Exists(filename)) | ||
{ | ||
scale = 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using absolute paths, the scale is hardcoded to 1, is fine in all the cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, scale = 1
is correct for absolute paths from MediaPicker. The scale parameter is for @2x/@3x bundle resources where you have specifically designed scaled variants. User photos from MediaPicker are already at their native resolution, so scale=1 tells iOS to interpret each pixel as 1 point, which is the correct behavior for displaying user-selected images.
Co-authored-by: jsuarezruiz <[email protected]>
Summary
Fixed an issue where images selected via MediaPicker were not displaying on iOS and macOS when using
FileImageSource
.Root Cause
The
GetPlatformImageSource
andGetPlatformImage
methods inImageSourceExtensions.cs
always attempted to search for files in the app bundle by prepending the bundle path, even for absolute paths. This caused MediaPicker-returned paths like/private/var/...
to be converted to invalid bundle paths.Solution
Added checks using
Path.IsPathRooted()
to detect absolute paths and handle them directly without bundle path manipulation:GetPlatformImage
: Load absolute paths directly viaUIImage.FromFile()
with File.Exists checkGetPlatformImageSource
: CreateNSUrl
from absolute paths without bundle search with File.Exists checkBuild Error Fix
Fixed CS0136 compiler error by removing the duplicate
url
variable declaration inside the if block, directly returning the CGImageSource instead.Code Review Updates
File.Exists()
check toGetPlatformImage()
for consistency withGetPlatformImageSource()
scale = 1
for absolute paths as this is correct for user-selected photos (they already have native resolution)Testing
Original prompt
This section details on the original issue you should resolve
<issue_title>Image is not displayed in Mac and IOS using Media Picker when it is placed in a Grid Layout in .NET 10</issue_title>
<issue_description>### Description
SignSample.zip
Description
When the "Upload Image" button is clicked, an image is selected using the Media Picker option and is expected to be displayed in a grid layout. This functionality works correctly on most platforms. However, on macOS and iOS, the selected image is not displayed as expected.
Please investigate the cause of this unexpected behavior on macOS and IOS.
Steps to Reproduce
Steps To Reproduce
Expected Behavior
The Image should be displayed in Grid Layout
Actual Behavior
The Image is not displayed in the Grid Layout
I will Provide the code Snippet
In MainPage.Xaml
`
`
`// MainPage.xaml.cs
using System.ComponentModel;
using Microsoft.Maui.Controls.Shapes;
namespace SignSample
{
public partial class MainPage : ContentPage
{
private readonly CustomSfView custom;
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.