private async void SendRequest() { try { using (ISitecoreWebApiSession session = this.instanceSettings.GetSession()) { IDownloadMediaOptions options = new MediaOptionsBuilder() .Set .Width(this.width) .Height(this.height) .BackgroundColor("white") .Build(); string path = this.MediaPathTextField.Text; var request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath(path) .DownloadOptions(options) .Build(); byte[] data = null; using (Stream response = await session.DownloadMediaResourceAsync(request)) using (MemoryStream responseInMemory = new MemoryStream()) { await response.CopyToAsync(responseInMemory); data = responseInMemory.ToArray(); } BeginInvokeOnMainThread(delegate { using (UIImage image = new UIImage(NSData.FromArray(data))) { // no need disposing // since this.ImageView.Image creates a // new C# object on each call this.ImageView.Image = image; // Update Overlay this.HideLoader(); } }); } } catch (Exception e) { AlertHelper.ShowLocalizedAlertWithOkOption("Error", e.Message); } finally { BeginInvokeOnMainThread(delegate { this.HideLoader(); }); } }
public async Task <Byte[]> GetMediaByUrl(string mediaUrl) { try { mediaUrl = CleanUpMediaUrlByReplacingWeirdTildeSignWithCorrect(mediaUrl); using (ISitecoreWebApiSession session = await SitecoreSession) { IMediaResourceDownloadRequest request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath(mediaUrl) .Language("en") .Build(); byte[] data = null; using (Stream response = await session.DownloadMediaResourceAsync(request)) using (MemoryStream responseInMemory = new MemoryStream()) { await response.CopyToAsync(responseInMemory); data = responseInMemory.ToArray(); return(data); } } } catch (SitecoreMobileSdkException ex) { this._loggingService.Log("Error in GetMediaByUrl, url {0} . Error: {1}", mediaUrl, ex.Message); throw ex; } catch (Exception ex) { this._loggingService.Log("Error in GetMediaByUrl, url {0} . Error: {1}", mediaUrl, ex.Message); throw ex; } }