public static Task <UIImage> LoadViaNuke(ImageSource source, CancellationToken token, float scale) { try { switch (source) { case UriImageSource uriSource: return(HandleUriSource(uriSource, token, scale)); case FileImageSource fileSource: return(FormsHandler.DisableFileImageSourceHandling ? ImageSourceHandler.DefaultFileImageSourceHandler.LoadImageAsync(fileSource, token, scale) : HandleFileSourceAsync(fileSource, token, scale)); default: FormsHandler.Warn($"Unhandled image source type {source.GetType().Name}"); break; } } catch (Exception exception) { FormsHandler.Warn($"Unexpected exception in Nuke image source handler: {exception}"); } return(Task.FromResult(default(UIImage))); }
public static async Task <UIImage> LoadViaNuke(ImageSource source, CancellationToken token) { try { switch (source) { case UriImageSource uriSource: var urlString = uriSource.Uri.OriginalString; if (String.IsNullOrEmpty(urlString)) { return(null); } FormsHandler.Debug("Loading `{0}` as a web URL", urlString); return(await LoadImageAsync(new NSUrl(urlString))); case FileImageSource fileSource: var fileName = fileSource.File; if (string.IsNullOrEmpty(fileName)) { return(null); } FormsHandler.Debug("Loading `{0}` as a file", fileName); NSUrl fileUrl = null; if (File.Exists(fileName)) { fileUrl = NSUrl.FromFilename(fileName);; } else { string name = Path.GetFileNameWithoutExtension(fileName); string extension = Path.GetExtension(fileName); FormsHandler.Debug($"Loading as bundle resource name: {name} with type: {extension}"); fileUrl = NSBundle.MainBundle.GetUrlForResource(name, extension); FormsHandler.Debug($"Bundle resource path: {fileUrl.AbsoluteString}"); } return(await LoadImageAsync(fileUrl)); //case StreamImageSource streamImageSource: // FormsHandler.Debug("Loading Image as a stream"); // return await LoadStreamAsync(streamImageSource); default: FormsHandler.Warn("Unhandled image source type {0}", source.GetType().Name); break; } } catch (Exception exception) { //Since developers can't catch this themselves, I think we should log it and silently fail FormsHandler.Warn("Unexpected exception in FFImageLoading image source handler: {0}", exception); } return(default(UIImage)); }
private static Task <UIImage> HandleFileSourceAsync(FileImageSource fileSource, CancellationToken token, float scale) { var fileName = fileSource.File; if (string.IsNullOrWhiteSpace(fileName)) { FormsHandler.Debug(() => "A null or empty filename has been specified for the FileImageSource, returning..."); return(null); } string name = Path.GetFileNameWithoutExtension(fileName); if (string.IsNullOrWhiteSpace(name)) { FormsHandler.Debug(() => $"An extension without a name ({fileName}) has been specified for the FileImageSource, returning..."); return(null); } string nameWithSuffix = $"{name}{ScaleToDensitySuffix[scale]}"; string filenameWithDensity = fileName.Replace(name, nameWithSuffix); if (token.IsCancellationRequested) { return(null); } NSUrl fileUrl; if (File.Exists(filenameWithDensity)) { FormsHandler.Debug(() => $"Loading \"{filenameWithDensity}\" as a file URI"); fileUrl = NSUrl.FromFilename(filenameWithDensity);; } else if (File.Exists(fileName)) { FormsHandler.Debug(() => $"Loading \"{fileName}\" as a file URI"); fileUrl = NSUrl.FromFilename(fileName);; } else { FormsHandler.Debug(() => $"Couldn't retrieve the image URI: loading \"{fileName}\" from Bundle"); return(Task.FromResult(UIImage.FromBundle(fileName))); } if (token.IsCancellationRequested) { return(null); } return(LoadImageAsync(fileUrl)); }
public Task <FormsCAKeyFrameAnimation> LoadImageAnimationAsync( ImageSource imageSource, CancellationToken cancellationToken = new CancellationToken(), float scale = 1) { FormsHandler.Debug(() => $"Delegating animation of {imageSource} to default Xamarin.Forms handler"); if (imageSource is UriImageSource) { return(DefaultUriImageSourceHandler.LoadImageAnimationAsync(imageSource, cancellationToken, scale)); } return(DefaultFileImageSourceHandler.LoadImageAnimationAsync(imageSource, cancellationToken, scale)); }
private static Task <UIImage> HandleUriSource(UriImageSource uriSource, CancellationToken token, float scale) { var urlString = uriSource.Uri?.OriginalString; if (string.IsNullOrWhiteSpace(urlString)) { FormsHandler.Debug(() => "A null or empty url has been specified for the UriImageSource, returning..."); return(null); } if (token.IsCancellationRequested) { return(null); } FormsHandler.Debug(() => $"Loading \"{urlString}\" as a web URL"); return(LoadImageAsync(new NSUrl(urlString))); }
private static Task <UIImage> LoadImageAsync(NSUrl url) { var tcs = new TaskCompletionSource <UIImage>(); ImagePipeline.Shared.LoadImageWithUrl( url, (image, errorMessage) => { if (image == null) { FormsHandler.Debug(() => $"Fail to load image: {url.AbsoluteString}, innerError: {errorMessage}"); } tcs.SetResult(image); }); return(tcs.Task); }
private static Task <UIImage> LoadPlaceholderAsync() { switch (FormsHandler.PlaceholderImageSource) { case StreamImageSource streamImageSource: FormsHandler.Warn($"loading placeholder from resource"); return(DefaultStreamImageSourceHandler.LoadImageAsync(streamImageSource)); ; case FontImageSource fontImageSource: FormsHandler.Warn($"loading placeholder from Font"); return(DefaultFontImageSourcehandler.LoadImageAsync(fontImageSource)); default: FormsHandler.Warn($"no valid placeholder found"); return(null); } }