public async Task <IImageSourceServiceResult <Image>?> GetImageAsync(IUriImageSource imageSource, Image image, CancellationToken cancellationToken = default) { if (imageSource.IsEmpty) { return(null); } var uri = imageSource.Uri; try { var isLoadComplated = await image.LoadAsync(uri, cancellationToken); if (!isLoadComplated) { throw new InvalidOperationException($"Unable to load image URI '{uri}'."); } return(new ImageSourceServiceResult(image)); } catch (Exception ex) { Logger?.LogWarning(ex, "Unable to load image URI '{Uri}'.", uri); throw; } }
public async Task <IImageSourceServiceResult <UIImage>?> GetImageAsync(IUriImageSource imageSource, float scale = 1, CancellationToken cancellationToken = default) { if (imageSource.IsEmpty) { return(null); } try { // TODO: use a real caching library with the URI if (imageSource is not IStreamImageSource streamImageSource) { return(null); } using var stream = await streamImageSource.GetStreamAsync(cancellationToken).ConfigureAwait(false); if (stream == null) { throw new InvalidOperationException($"Unable to load image stream from URI '{imageSource.Uri}'."); } var data = NSData.FromStream(stream); if (data == null) { throw new InvalidOperationException("Unable to load image stream data."); } var image = UIImage.LoadFromData(data, scale); if (image == null) { throw new InvalidOperationException($"Unable to decode image from URI '{imageSource.Uri}'."); } var result = new ImageSourceServiceResult(image, () => image.Dispose()); return(result); } catch (Exception ex) { Logger?.LogWarning(ex, "Unable to load image URI '{Uri}'.", imageSource.Uri); throw; } }
public async Task <IImageSourceServiceResult <Drawable>?> GetDrawableAsync(IUriImageSource imageSource, Context context, CancellationToken cancellationToken = default) { if (imageSource.IsEmpty) { return(null); } var uri = imageSource.Uri; try { var builder = Glide .With(context) .Load(uri.OriginalString); if (!imageSource.CachingEnabled) { builder = builder .SetDiskCacheStrategy(DiskCacheStrategy.None) .SkipMemoryCache(true); } var result = await builder .SubmitAsync(context, cancellationToken) .ConfigureAwait(false); if (result == null) { throw new InvalidOperationException($"Unable to load image URI '{uri}'."); } return(result); } catch (Exception ex) { Logger?.LogWarning(ex, "Unable to load image URI '{Uri}'.", uri); throw; } }
public async Task <IImageSourceServiceResult <UIImage>?> GetImageAsync(IUriImageSource imageSource, float scale = 1, CancellationToken cancellationToken = default) { if (imageSource.IsEmpty) { return(null); } try { var hash = Crc64.ComputeHashString(imageSource.Uri.OriginalString); var pathToImageCache = CacheDirectory + hash + ".png"; NSData?imageData; if (imageSource.CachingEnabled && IsImageCached(pathToImageCache)) { imageData = GetCachedImage(pathToImageCache); } else { // TODO: use a real caching library with the URI if (imageSource is not IStreamImageSource streamImageSource) { return(null); } using var stream = await streamImageSource.GetStreamAsync(cancellationToken).ConfigureAwait(false); if (stream == null) { throw new InvalidOperationException($"Unable to load image stream from URI '{imageSource.Uri}'."); } imageData = NSData.FromStream(stream); if (imageData == null) { throw new InvalidOperationException("Unable to load image stream data."); } if (imageSource.CachingEnabled) { CacheImage(imageData, pathToImageCache); } } var image = UIImage.LoadFromData(imageData, scale); if (image == null) { throw new InvalidOperationException($"Unable to decode image from URI '{imageSource.Uri}'."); } var result = new ImageSourceServiceResult(image, () => image.Dispose()); return(result); } catch (Exception ex) { Logger?.LogWarning(ex, "Unable to load image URI '{Uri}'.", imageSource.Uri); throw; } }
public async Task <IImageSourceServiceResult <WImageSource>?> GetImageSourceAsync(IUriImageSource imageSource, float scale = 1, CancellationToken cancellationToken = default) { if (imageSource.IsEmpty) { return(null); } // TODO: use a real caching library with the URI if (imageSource is not IStreamImageSource streamImageSource) { throw new InvalidOperationException("Unable to load URI as a stream."); } try { using var stream = await streamImageSource.GetStreamAsync(cancellationToken); if (stream == null) { throw new InvalidOperationException("Unable to load image stream."); } var image = new BitmapImage(); using var ras = stream.AsRandomAccessStream(); await image.SetSourceAsync(ras); var result = new ImageSourceServiceResult(image); return(result); } catch (Exception ex) { Logger?.LogWarning(ex, "Unable to load image URI '{Uri}'.", imageSource.Uri); throw; } }