resizedImage.UnlockFocus();
return resizedImage;
}
-
- internal static async Task<NSImage> GetNativeImageAsync(this ImageSource source, CancellationToken cancellationToken = default(CancellationToken))
- {
- if (source == null || source.IsEmpty)
- return null;
-
- var handler = Internals.Registrar.Registered.GetHandlerForObject<IImageSourceHandler>(source);
- if (handler == null)
- return null;
-
- try
- {
- return await handler.LoadImageAsync(source, scale: (float)NSScreen.MainScreen.BackingScaleFactor, cancelationToken: cancellationToken);
- }
- catch (OperationCanceledException)
- {
- Log.Warning("Image loading", "Image load cancelled");
- }
- catch (Exception ex)
- {
- Log.Warning("Image loading", $"Image load failed: {ex}");
- }
-
- return null;
- }
-
- internal static Task ApplyNativeImageAsync(this IVisualElementRenderer renderer, BindableProperty imageSourceProperty, Action<NSImage> onSet, Action<bool> onLoading = null, CancellationToken cancellationToken = default(CancellationToken))
- {
- return renderer.ApplyNativeImageAsync(null, imageSourceProperty, onSet, onLoading, cancellationToken);
- }
-
- internal static async Task ApplyNativeImageAsync(this IVisualElementRenderer renderer, BindableObject bindable, BindableProperty imageSourceProperty, Action<NSImage> onSet, Action<bool> onLoading = null, CancellationToken cancellationToken = default(CancellationToken))
- {
- _ = renderer ?? throw new ArgumentNullException(nameof(renderer));
- _ = imageSourceProperty ?? throw new ArgumentNullException(nameof(imageSourceProperty));
- _ = onSet ?? throw new ArgumentNullException(nameof(onSet));
-
- // TODO: it might be good to make sure the renderer has not been disposed
-
- // makse sure things are good before we start
- var element = bindable ?? renderer.Element;
-
- var nativeRenderer = renderer as IVisualNativeElementRenderer;
-
- if (element == null || renderer.NativeView == null || (nativeRenderer != null && nativeRenderer.Control == null))
- return;
-
- onLoading?.Invoke(true);
- if (element.GetValue(imageSourceProperty) is ImageSource initialSource && !initialSource.IsEmpty)
- {
- try
- {
- using (var drawable = await initialSource.GetNativeImageAsync(cancellationToken))
- {
- // TODO: it might be good to make sure the renderer has not been disposed
-
- // we are back, so update the working element
- element = bindable ?? renderer.Element;
-
- // makse sure things are good now that we are back
- if (element == null || renderer.NativeView == null || (nativeRenderer != null && nativeRenderer.Control == null))
- return;
-
- // only set if we are still on the same image
- if (element.GetValue(imageSourceProperty) == initialSource)
- onSet(drawable);
- }
- }
- finally
- {
- if (element != null && onLoading != null)
- {
- // only mark as finished if we are still on the same image
- if (element.GetValue(imageSourceProperty) == initialSource)
- onLoading.Invoke(false);
- }
- }
- }
- else
- {
- onSet(null);
- onLoading?.Invoke(false);
- }
- }
-
- internal static async Task ApplyNativeImageAsync(this BindableObject bindable, BindableProperty imageSourceProperty, Action<NSImage> onSet, Action<bool> onLoading = null, CancellationToken cancellationToken = default(CancellationToken))
- {
- _ = bindable ?? throw new ArgumentNullException(nameof(bindable));
- _ = imageSourceProperty ?? throw new ArgumentNullException(nameof(imageSourceProperty));
- _ = onSet ?? throw new ArgumentNullException(nameof(onSet));
-
- onLoading?.Invoke(true);
- if (bindable.GetValue(imageSourceProperty) is ImageSource initialSource)
- {
- try
- {
- using (var nsimage = await initialSource.GetNativeImageAsync(cancellationToken))
- {
- // only set if we are still on the same image
- if (bindable.GetValue(imageSourceProperty) == initialSource)
- onSet(nsimage);
- }
- }
- finally
- {
- if (onLoading != null)
- {
- // only mark as finished if we are still on the same image
- if (bindable.GetValue(imageSourceProperty) == initialSource)
- onLoading.Invoke(false);
- }
- }
- }
- else
- {
- onSet(null);
- onLoading?.Invoke(false);
- }
- }
}
}
\ No newline at end of file
using System.Threading;
using System.Threading.Tasks;
using Foundation;
-using UIKit;
using Xamarin.Forms.Internals;
#if __MOBILE__
try
{
+
#if __MOBILE__
- float scale = (float)UIScreen.MainScreen.Scale;
+ float scale = (float)UIScreen.MainScreen.Scale;
#else
- float scale = (float)NSScreen.MainScreen.BackingScaleFactor;
+ float scale = (float)NSScreen.MainScreen.BackingScaleFactor;
#endif
- return await handler.LoadImageAsync(source, scale: (float)UIScreen.MainScreen.Scale, cancelationToken: cancellationToken);
+
+ return await handler.LoadImageAsync(source, scale: scale, cancelationToken: cancellationToken);
}
catch (OperationCanceledException)
{
return null;
}
+#if __MOBILE__
internal static Task ApplyNativeImageAsync(this IShellContext shellContext, BindableObject bindable, BindableProperty imageSourceProperty, Action<UIImage> onSet, Action<bool> onLoading = null, CancellationToken cancellationToken = default(CancellationToken))
{
_ = shellContext ?? throw new ArgumentNullException(nameof(shellContext));
return renderer.ApplyNativeImageAsync(bindable, imageSourceProperty, onSet, onLoading, cancellationToken);
}
-
- internal static Task ApplyNativeImageAsync(this IVisualElementRenderer renderer, BindableProperty imageSourceProperty, Action<UIImage> onSet, Action<bool> onLoading = null, CancellationToken cancellationToken = default(CancellationToken))
+#endif
+ internal static Task ApplyNativeImageAsync(this IVisualElementRenderer renderer, BindableProperty imageSourceProperty, Action<NativeImage> onSet, Action<bool> onLoading = null, CancellationToken cancellationToken = default(CancellationToken))
{
return renderer.ApplyNativeImageAsync(null, imageSourceProperty, onSet, onLoading, cancellationToken);
}
- internal static async Task ApplyNativeImageAsync(this IVisualElementRenderer renderer, BindableObject bindable, BindableProperty imageSourceProperty, Action<UIImage> onSet, Action<bool> onLoading = null, CancellationToken cancellationToken = default(CancellationToken))
+ internal static async Task ApplyNativeImageAsync(this IVisualElementRenderer renderer, BindableObject bindable, BindableProperty imageSourceProperty, Action<NativeImage> onSet, Action<bool> onLoading = null, CancellationToken cancellationToken = default(CancellationToken))
{
_ = renderer ?? throw new ArgumentNullException(nameof(renderer));
_ = imageSourceProperty ?? throw new ArgumentNullException(nameof(imageSourceProperty));
}
}
- internal static async Task ApplyNativeImageAsync(this BindableObject bindable, BindableProperty imageSourceProperty, Action<UIImage> onSet, Action<bool> onLoading = null, CancellationToken cancellationToken = default(CancellationToken))
+ internal static async Task ApplyNativeImageAsync(this BindableObject bindable, BindableProperty imageSourceProperty, Action<NativeImage> onSet, Action<bool> onLoading = null, CancellationToken cancellationToken = default(CancellationToken))
{
_ = bindable ?? throw new ArgumentNullException(nameof(bindable));
_ = imageSourceProperty ?? throw new ArgumentNullException(nameof(imageSourceProperty));