public override void ViewWillAppear(bool animated) { base.ViewWillAppear(animated); propertyManager = new PropertyMangager(); if (currentPropertyId > 0) { propertyDetail = propertyManager.GetItemAsync(currentPropertyId.ToString()).Result; byte[] imageBytes = null; imageBytes = propertyManager.GetImageAsync(propertyDetail.Image).Result; //ImageHelper.SetImage(imageBytes, propertyDetail.ListingID, PropertyImage, 150); ImageHelper.SetImage(propertyManager, propertyDetail.ListingID, propertyDetail.Image, PropertyImage); AddressLabel.Text = propertyDetail.Address; BedsLabel.Text = string.Format("Beds: {0}", propertyDetail.Beds); BathsLabel.Text = string.Format(", Baths: {0}", propertyDetail.Baths); EstimatedValueLabel.Text = string.Format(", {0:C}, ", propertyDetail.EstimatedValue); RateChangeLabel.Text = string.Format("{0}%", Convert.ToString(propertyDetail.ChangeOverLastYear)); FeatureText.Text = propertyDetail.Features; if (Convert.ToDouble(propertyDetail.ChangeOverLastYear) < 0) { RateChangeLabel.TextColor = UIColor.Red; } } }
/// <summary> /// Download the image from the url and save it to local storage for reuse /// </summary> /// <param name="propertyManager"></param> /// <param name="listingID"></param> /// <param name="imageName"></param> /// <param name="downloadedImageView"></param> /// <param name="isOrignal"></param> public static void SetImage(IPropertyMangager propertyManager, int listingID, string imageName, UIImageView downloadedImageView, int isOrignal = 50) { byte[] imageBytes = null; //imageBytes = propertyManager.GetImageAsync(propertyitem.Image).Result if (!urlToImageMap.ContainsKey(listingID)) { string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); string localFilename = listingID + ".jpg"; string localPath = System.IO.Path.Combine(documentsPath, localFilename); if (!File.Exists(localPath)) { Task.Run(async() => { imageBytes = await propertyManager.GetImageAsync(imageName); } ).ConfigureAwait(false).GetAwaiter().GetResult(); File.WriteAllBytes(localPath, imageBytes); // writes to local storage } downloadedImageView.Image = UIImage.FromFile(localPath); urlToImageMap.Add(listingID, localPath); } else { downloadedImageView.Image = UIImage.FromFile(urlToImageMap[listingID]); } }
/// <summary> /// Download the image from the url and save it to local storage for reuse /// </summary> /// <param name="propertyManager"></param> /// <param name="listingID"></param> /// <param name="imageName"></param> /// <param name="downloadedImageView"></param> /// <param name="isOrignal"></param> public static void SetImage(IPropertyMangager propertyManager, int listingID, string imageName, ImageView downloadedImageView, int isOrignal = 100) { byte[] imageBytes = null; if (!urlToImageMap.ContainsKey(listingID)) { string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); string localFilename = listingID + ".jpg"; string localPath = System.IO.Path.Combine(documentsPath, localFilename); if (!File.Exists(localPath)) { Task.Run(async() => { imageBytes = await propertyManager.GetImageAsync(imageName); } ).GetAwaiter().GetResult(); if (imageBytes != null && imageBytes.Length > 0) { File.WriteAllBytes(localPath, imageBytes); // writes to local storage } } var localImage = new Java.IO.File(localPath); if (localImage.Exists()) { SetPic(localImage.AbsolutePath, downloadedImageView, isOrignal); urlToImageMap.Add(listingID, localImage.AbsolutePath); } } else { SetPic(urlToImageMap[listingID], downloadedImageView, isOrignal); } }
public void TestGetImageAsync() { Task.Run(async() => { var image = await mgr.GetImageAsync("/images/1.jpg"); Assert.IsTrue(image != null && image.Length > 0); } ).GetAwaiter().GetResult(); }