/// <summary> /// Set the source URI of the image. /// </summary> /// <param name="view">The image view instance.</param> /// <param name="source">The source URI.</param> private async void SetUriFromSingleSource(Border view, string source) { var imageBrush = (ImageBrush)view.Background; var tag = view.GetTag(); var disposable = default(SerialDisposable); if (!_disposables.TryGetValue(tag, out disposable)) { disposable = new SerialDisposable(); _disposables.Add(tag, disposable); } if (BitmapImageHelpers.IsBase64Uri(source)) { var image = new BitmapImage(); disposable.Disposable = image.GetStreamLoadObservable().Subscribe( status => OnImageStatusUpdate(view, status.LoadStatus, status.Metadata), _ => OnImageFailed(view)); using (var stream = await BitmapImageHelpers.GetStreamAsync(source)) { await image.SetSourceAsync(stream); } imageBrush.ImageSource = image; } else if (BitmapImageHelpers.IsHttpUri(source)) { OnImageStatusUpdate(view, ImageLoadStatus.OnLoadStart, default(ImageMetadata)); try { var image = await ImageCache.Instance.GetFromCacheAsync(new Uri(source), true); var metadata = new ImageMetadata(source, image.PixelWidth, image.PixelHeight); OnImageStatusUpdate(view, ImageLoadStatus.OnLoad, metadata); imageBrush.ImageSource = image; OnImageStatusUpdate(view, ImageLoadStatus.OnLoadEnd, metadata); } catch { OnImageFailed(view); } } else { var image = new BitmapImage(); disposable.Disposable = image.GetUriLoadObservable().Subscribe( status => OnImageStatusUpdate(view, status.LoadStatus, status.Metadata), _ => OnImageFailed(view)); image.UriSource = new Uri(source); imageBrush.ImageSource = image; } }
/// <summary> /// Set the source URI of the image. /// </summary> /// <param name="view">The image view instance.</param> /// <param name="source">The source URI.</param> private async void SetUriFromSingleSource(Image view, string source) { Log.Info(ReactConstants.Tag, "[SetUriFromSingleSource] set src:" + source); var tag = view.GetTag(); var disposable = default(SerialDisposable); if (!_disposables.TryGetValue(tag, out disposable)) { disposable = new SerialDisposable(); _disposables.Add(tag, disposable); } OnImageStatusUpdate(view, ImageLoadStatus.OnLoadStart, default(ImageMetadata)); var metadata = new ImageMetadata(source, view.GetDimensions().Width, view.GetDimensions().Height); if (BitmapImageHelpers.IsBase64Uri(source)) { Log.Info(ReactConstants.Tag, ">>> Base64Uri"); //using (var stream = await BitmapImageHelpers.GetStreamAsync(source)) using (var stream = BitmapImageHelpers.GetStreamSync(source)) { var ret = await view.LoadAsync(stream); if (false == ret) { Log.Warn(ReactConstants.Tag, "Failed to LoadAsync:" + source.ToString()); OnImageFailed(view); } else { OnImageStatusUpdate(view, ImageLoadStatus.OnLoad, metadata); } } } else if (BitmapImageHelpers.IsHttpUri(source)) { Log.Info(ReactConstants.Tag, ">>> HttpUri"); //var metadata = new ImageMetadata(source, view.GetDimensions().Width, view.GetDimensions().Height); var ret = await view.LoadAsync(source); if (false == ret) { Log.Warn(ReactConstants.Tag, "Failed to LoadAsync:" + source.ToString()); OnImageFailed(view); } else { OnImageStatusUpdate(view, ImageLoadStatus.OnLoad, metadata); } } else { Log.Info(ReactConstants.Tag, ">>> Loacal"); //var metadata = new ImageMetadata(source, view.GetDimensions().Width, view.GetDimensions().Height); var ret = await view.LoadAsync(source); if (false == ret) { Log.Warn(ReactConstants.Tag, "Failed to LoadAsync:" + source.ToString()); OnImageFailed(view); } else { OnImageStatusUpdate(view, ImageLoadStatus.OnLoad, metadata); } } OnImageStatusUpdate(view, ImageLoadStatus.OnLoadEnd, metadata); disposable.Dispose(); }