private async void RequestImageForCell(UIEntityTableViewCell <TEntity> cell, NSIndexPath indexPath) { UIImage image = this.TryGetImageForCell(cell); if (image != null) { cell.SetImage(image); } else { string imageUrl = cell.ImageUrl; RemoteImageRequest imageRequest = this.imageCatalog.StartImageRequest(imageUrl, cell.ImageOptions); if (!imageRequest.IsRequestCompleted) { cell.SetImageDownloadInProgress(); } ServiceResult result = await imageRequest.Task; // Check if the requesting cell was new and ensure that it hasn't already been reused. if (indexPath.Length == 0 && imageUrl == cell.ImageUrl) { this.SetImageFromResult(result, cell); } else { this.SetImageFromResult(result, indexPath); } } }
private void SetImageFromResult(ServiceResult result, UIEntityTableViewCell <TEntity> cell) { if (cell != null) { if (result.ResultCode == ServiceResultCode.Success) { UIImage image = this.TryGetImageForCell(cell); if (image == null) { // is it possible the image was automatically removed from the cache in such a short period of time? throw new InvalidOperationException("An image was not found in the cache."); } cell.SetImage(image); } else { UIImage errorImage = result.ResultCode == ServiceResultCode.NetworkUnavailable ? this.GetDisconnectedImage() : this.GetImageDownloadFailedImage(result); UIViewContentMode contentMode = UIViewContentMode.Center; UIImageView imageView = cell.GetImageView(); if (errorImage != null && (errorImage.Size.Width > imageView.Bounds.Width || errorImage.Size.Height > imageView.Bounds.Height)) { contentMode = UIViewContentMode.ScaleAspectFit; } cell.SetImage(errorImage, contentMode); } cell.MarkImageDownloadComplete(result.ResultCode == ServiceResultCode.Success); } }