private async Task <MemoryStream> GetPhoto(Attachment attachment, SignWith signWith, DateTime now) { // 1. Found in cache? if (this.imageCacheService.TryGet(attachment.Url, out MemoryStream stream)) { return(stream); } // 2. Needs download KeyValuePair <string, TemporaryFile> pair = await this.neuronService.Contracts.GetAttachment(attachment.Url, signWith, Constants.Timeouts.DownloadFile); // If download has been cancelled any time _during_ download, stop here. if (this.loadPhotosTimestamp > now) { pair.Value.Dispose(); return(null); } using (TemporaryFile file = pair.Value) { file.Reset(); MemoryStream ms = new MemoryStream(); await file.CopyToAsync(ms); // 3. Save to cache ms.Reset(); await this.imageCacheService.Add(attachment.Url, ms); ms.Reset(); return(ms); } }
private async void LoadPhotos() { if (!(this.identity.Attachments is null)) { int i = this.TableView.Root.IndexOf(this.ButtonSection); TableSection PhotoSection = new TableSection(); this.TableView.Root.Insert(i++, PhotoSection); foreach (Attachment Attachment in this.identity.Attachments) { if (Attachment.ContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase)) { ViewCell ViewCell; try { KeyValuePair <string, TemporaryFile> P = await App.Contracts.GetAttachmentAsync(Attachment.Url, 10000); using (TemporaryFile File = P.Value) { MemoryStream ms = new MemoryStream(); File.Position = 0; await File.CopyToAsync(ms); ms.Position = 0; ViewCell = new ViewCell() { View = new Image() { Source = ImageSource.FromStream(() => ms) } }; } } catch (Exception ex) { ViewCell = new ViewCell() { View = new Label() { Text = ex.Message } }; } await Device.InvokeOnMainThreadAsync(() => { PhotoSection.Add(ViewCell); }); } } } }