Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
x:Class="Maui.Controls.Sample.Pages.MapsGalleries.CustomPinIconGallery"
Title="Custom Pin Icons">
<Grid RowDefinitions="Auto,*">
<StackLayout Orientation="Horizontal" Padding="10" Spacing="10">
<Button Text="Add Custom Pins" Clicked="OnAddCustomPinsClicked"/>
<Button Text="Add Default Pin" Clicked="OnAddDefaultPinClicked"/>
<Button Text="Clear" Clicked="OnClearClicked"/>
</StackLayout>

<FlexLayout Wrap="Wrap" Padding="10" AlignItems="Center">
<Button Text="Add Custom Pins" Clicked="OnAddCustomPinsClicked" Margin="5"/>
<Button Text="Add Default Pin" Clicked="OnAddDefaultPinClicked" Margin="5"/>
<Button Text="Toggle Icon" Clicked="OnToggleIconClicked" Margin="5"/>
<Button Text="Move &amp; Rename" Clicked="OnMoveRenameClicked" Margin="5"/>
<Button Text="Clear" Clicked="OnClearClicked" Margin="5"/>
</FlexLayout>

<maps:Map x:Name="CustomPinMap" Grid.Row="1"/>
</Grid>
</ContentPage>
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ namespace Maui.Controls.Sample.Pages.MapsGalleries
{
public partial class CustomPinIconGallery : ContentPage
{
bool _altIcon;
int _moveCount;

public CustomPinIconGallery()
{
InitializeComponent();

// Center on Seattle
CustomPinMap.MoveToRegion(MapSpan.FromCenterAndRadius(
new Microsoft.Maui.Devices.Sensors.Location(47.6062, -122.3321),
Expand Down Expand Up @@ -54,6 +57,34 @@ void OnAddDefaultPinClicked(object? sender, EventArgs e)
CustomPinMap.Pins.Add(defaultPin);
}

// Swaps ImageSource on every existing pin at runtime, to test whether the marker icon
// refreshes live. Toggles between two distinct bundled images so the change is obvious.
void OnToggleIconClicked(object? sender, EventArgs e)
{
_altIcon = !_altIcon;
var file = _altIcon ? "coffee.png" : "dotnet_bot.png";

foreach (var pin in CustomPinMap.Pins)
{
pin.ImageSource = ImageSource.FromFile(file);
}
}

// Control case: Location and Label have real per-platform mappers, so these should update
// live on every platform (unlike ImageSource) — used to contrast the ImageSource behavior.

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] Sample Accuracy — The comment still says Location/Label update live "unlike ImageSource", but this gallery now exists to verify ImageSource also refreshes live at runtime. Updating the wording avoids sending future testers toward the old expected behavior.

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.

Fixed in 6ce52cf. Reworded to note Location/Label update live via their mappers and to use this button alongside Toggle Icon to confirm ImageSource now refreshes live too.

void OnMoveRenameClicked(object? sender, EventArgs e)
{
_moveCount++;

foreach (var pin in CustomPinMap.Pins)
{
pin.Location = new Microsoft.Maui.Devices.Sensors.Location(
pin.Location.Latitude + 0.01,
pin.Location.Longitude);
pin.Label = $"Moved x{_moveCount}";
}
}

void OnClearClicked(object? sender, EventArgs e)
{
CustomPinMap.Pins.Clear();
Expand Down
9 changes: 6 additions & 3 deletions src/Core/maps/src/Handlers/MapPin/MapPinHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using CoreLocation;
using MapKit;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Maps.Platform;

namespace Microsoft.Maui.Maps.Handlers
{
Expand All @@ -27,11 +28,13 @@ public static void MapAddress(IMapPinHandler handler, IMapPin mapPin)
mKPointAnnotation.Subtitle = mapPin.Address;
}

// Note: ImageSource is handled in MauiMKMapView.GetViewForAnnotation
// because the image is set on the MKAnnotationView, not on the IMKAnnotation
// The initial image is applied in MauiMKMapView.GetViewForAnnotation (set on the
// MKAnnotationView, not the IMKAnnotation). This handles runtime ImageSource changes by
// refreshing the annotation view of a pin that is already on the map.
public static void MapImageSource(IMapPinHandler handler, IMapPin mapPin)
{
// No-op: Image is applied when the annotation view is created in GetViewForAnnotation
if (mapPin.Parent?.Handler?.PlatformView is MauiMKMapView mapView)
mapView.UpdatePinImage(mapPin);
}
}
}
23 changes: 19 additions & 4 deletions src/Core/maps/src/Platform/iOS/MauiMKMapView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,21 +248,36 @@ void ZoomToShowClusterPins(IMKAnnotation[] annotations)
SetVisibleMapRect(paddedRect, true);
}

// Refreshes a pin already on the map after its ImageSource changed at runtime, updating the
// live annotation view in place. Pins with no individual view (off-screen or collapsed into a
// cluster) are skipped; GetViewForAnnotation applies the current ImageSource when they appear.
internal void UpdatePinImage(IMapPin pin)

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)

[moderate] Regression Prevention — UpdatePinImage introduces the runtime ImageSource refresh path for iOS/MacCatalyst but no automated device test covers it. The three key transitions—custom→custom swap, custom→null (revert to platform default), and null→custom (first assignment after pin is already on map)—are exercised only by the new manual gallery buttons. A device test that adds a pin, changes its ImageSource through each boundary, and asserts the annotation view type via ViewForAnnotation would close this regression gap (the pre-flight review flagged this gap independently).

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.

Added in 09934f1: a device test in MapTests.iOS.cs ("Pin ImageSource Runtime Change Updates Annotation View") covering the three transitions on a pin already on the map — null→custom asserts the view swaps to a custom MKAnnotationView with a loaded image, custom→custom asserts the image instance changes in place, custom→null asserts the view reverts to MKMarkerAnnotationView. The annotation is re-resolved on every poll since boundary transitions remove/re-add it, and the platform view is given an explicit frame because the test harness attaches it unsized and MapKit only materializes annotation views for a laid-out map.

{
if (pin.MarkerId is not IMKAnnotation annotation || ViewForAnnotation(annotation) is not MKAnnotationView view)
return;

view.Image = null;
if (pin.ImageSource is not null)
Comment thread
kevin68 marked this conversation as resolved.
Resolved
ApplyCustomImageAsync(view, pin).FireAndForget();
}

async System.Threading.Tasks.Task ApplyCustomImageAsync(MKAnnotationView annotationView, IMapPin pin)
{
_handlerRef.TryGetTarget(out IMapHandler? handler);
if (handler?.MauiContext == null || pin.ImageSource == null)
return;

// Capture the annotation before the async operation to detect reuse
// Capture the annotation and requested source before the async operation, to detect both
// view reuse and a newer ImageSource change that started while this load was in flight.
var targetAnnotation = annotationView.Annotation;
var requestedSource = pin.ImageSource;

try
{
var result = await pin.ImageSource.GetPlatformImageAsync(handler.MauiContext);
var result = await requestedSource.GetPlatformImageAsync(handler.MauiContext);

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)

[major] Memory Leak Prevention — The IImageSourceServiceResult<UIImage> returned by GetPlatformImageAsync needs to be disposed after the scaled copy is created. Several iOS image services attach a dispose callback to release the loaded UIImage (for example URI/stream image sources), and this new runtime refresh path can run repeatedly as Pin.ImageSource changes, leaking each loaded native image. Please use using var result = await requestedSource.GetPlatformImageAsync(...) (or otherwise dispose it after ScaleImage) so repeated icon updates do not retain the source images.

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.

Fixed in 981e5df. ApplyCustomImageAsync now uses using var result = await requestedSource.GetPlatformImageAsync(...), so the image service's dispose callback runs after ScaleImage has produced the copy assigned to the annotation view. Since the runtime refresh path shares this method, repeated Pin.ImageSource changes no longer retain each loaded native image.


// Verify the annotation view hasn't been reused for a different pin
if (annotationView.Annotation != targetAnnotation)
// Drop this load if the view was reused or the pin's ImageSource changed since.
if (annotationView.Annotation != targetAnnotation || !ReferenceEquals(pin.ImageSource, requestedSource))

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)

[moderate] iOS/MacCatalyst Platform Specifics — guard the annotation view handle before reading it after the async load

UpdatePinImage can now start a custom-image load for a visible pin and then immediately remove/re-add that annotation when ImageSource crosses the null/non-null boundary. If the earlier load completes after MapKit has released that reused annotation view, this post-await check still calls annotationView.Annotation on a potentially zero-handle UIKit wrapper. Treat disposal as a normal stale-result case instead of relying on the catch/log path: add if (annotationView.Handle == IntPtr.Zero) return; before reading annotationView.Annotation.

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.

Fixed in 429ffab. The post-await staleness check now tests annotationView.Handle == IntPtr.Zero first, so a view released by the remove/re-add path while a load was in flight is treated as a normal stale result and skipped, instead of throwing from annotationView.Annotation into the catch block.

return;

if (result?.Value is UIImage image)
Expand Down
Loading