Ejemplo n.º 1
0
        //public static void SetImageFromNSUrlSession(string imagePath, UIImageView imageView, NSCache imageCache = null)
        //{
        //    UIImage imageFromCache = null;
        //    var url = new NSUrl(EnvironmentConstants.getS3Url() + imagePath);
        //    var noCacheStr = "?nocache=" + String.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now);
        //    var fetchUrl = new NSUrl(EnvironmentConstants.getS3Url() + imagePath + noCacheStr);

        //    if (imageCache != null)
        //    {
        //        imageFromCache = (UIImage)imageCache.ObjectForKey(NSString.FromObject(url.AbsoluteString));
        //    }

        //    if (imageFromCache != null)
        //    {
        //        imageView.Image = imageFromCache;
        //    }
        //    else
        //    {
        //        var task = NSUrlSession.SharedSession.CreateDataTask(fetchUrl, (data, response, error) =>
        //        {
        //            try
        //            {
        //                DispatchQueue.MainQueue.DispatchAsync(() =>
        //                {
        //                    if (response != null && ((NSHttpUrlResponse)response).StatusCode != 403 && error == null)
        //                    {
        //                        if (imageCache != null)
        //                        {
        //                            var imageToCache = UIImage.LoadFromData(data);

        //                            imageView.Image = imageToCache;

        //                            if (imageToCache != null)
        //                            {
        //                                imageCache.SetObjectforKey(imageToCache, NSString.FromObject(url.AbsoluteString));
        //                            }
        //                        }
        //                        else
        //                        {
        //                            imageView.Image = UIImage.LoadFromData(data);
        //                        }
        //                    }
        //                });
        //            }
        //            catch (Exception ex)
        //            {
        //                Utils.HandleException(ex);
        //            }
        //        });
        //        task.Resume();
        //    }
        //}

        public static void SetImageFromNSUrlSession(string imagePath, UIImageView imageView, NSObject controller, PictureType picType)
        {
            UIImage placeholder = UIImage.FromBundle("ImagePlaceholder");

            try
            {
                if (picType.Equals(PictureType.Profile))
                {
                    placeholder = UIImage.FromBundle("Profile");
                }

                if (picType.Equals(PictureType.Group))
                {
                    placeholder = UIImage.FromBundle("Group");
                }

                imageView.SetImage(new NSUrl(EnvironmentConstants.getS3Url() + imagePath), placeholder, SDWebImageOptions.ProgressiveDownload, //HighPriority
                                   progressBlock: (receivedSize, completedSize) =>
                {
                    if (activityIndicator == null)
                    {
                        controller.InvokeOnMainThread(() =>
                        {
                            activityIndicator = new UIActivityIndicatorView(UIActivityIndicatorViewStyle.Gray);
                            imageView.AddSubview(activityIndicator);
                            activityIndicator.Center = imageView.Center;
                            activityIndicator.StartAnimating();
                        });
                    }
                },
                                   completedBlock: (image, error, cacheType, finished) =>
                {
                    Console.WriteLine("Image = " + image);
                    Console.WriteLine("Error = " + error);
                    Console.WriteLine("Cache Type = " + cacheType);
                    Console.WriteLine("Finished = " + finished);

                    if (activityIndicator != null)
                    {
                        controller.InvokeOnMainThread(() =>
                        {
                            activityIndicator.RemoveFromSuperview();
                            activityIndicator = null;
                        });
                    }

                    if (error != null && error.ToString().Contains("1100"))
                    {
                        imageView.SetImage(new NSUrl(EnvironmentConstants.getS3Url() + imagePath), placeholder, SDWebImageOptions.RetryFailed);
                    }

                    if (image != null)
                    {
                        imageView.Image = image;
                    }
                });
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }