/// <summary> /// Loads and gets UImage using defined parameters. /// IMPORTANT: It throws image loading exceptions - you should handle them /// </summary> /// <returns>The PImage async.</returns> /// <param name="parameters">Parameters.</param> public static Task <NSImage> AsNSImageAsync(this TaskParameter parameters) { var target = new PImageTarget(); var userErrorCallback = parameters.OnError; var finishCallback = parameters.OnFinish; var tcs = new TaskCompletionSource <NSImage>(); parameters .Error(ex => { tcs.TrySetException(ex); userErrorCallback?.Invoke(ex); }) .Finish(scheduledWork => { finishCallback?.Invoke(scheduledWork); tcs.TrySetResult(target.PImage); }); if (parameters.Source != ImageSource.Stream && string.IsNullOrWhiteSpace(parameters.Path)) { target.SetAsEmpty(null); parameters.TryDispose(); return(null); } var task = ImageService.CreateTask(parameters, target); ImageService.Instance.LoadImage(task); return(tcs.Task); }
/// <summary> /// Loads and gets PImage using defined parameters. /// IMPORTANT: It throws image loading exceptions - you should handle them /// </summary> /// <returns>The PImage async.</returns> /// <param name="parameters">Parameters.</param> /// <param name="imageScale">Image scale.</param> public static Task <PImage> AsPImageAsync(this TaskParameter parameters, float imageScale = -1f) { var target = new PImageTarget(); var userErrorCallback = parameters.OnError; var finishCallback = parameters.OnFinish; var tcs = new TaskCompletionSource <PImage>(); List <Exception> exceptions = null; parameters .Error(ex => { if (exceptions == null) { exceptions = new List <Exception>(); } exceptions.Add(ex); userErrorCallback?.Invoke(ex); }) .Finish(scheduledWork => { finishCallback?.Invoke(scheduledWork); if (exceptions != null) { tcs.TrySetException(exceptions); } else { tcs.TrySetResult(target.PImage); } }); if (parameters.Source != ImageSource.Stream && string.IsNullOrWhiteSpace(parameters.Path)) { target.SetAsEmpty(null); parameters.TryDispose(); return(null); } var task = CreateTask(parameters, imageScale, target); ImageService.Instance.LoadImage(task); return(tcs.Task); }